8

Tôi có LinearLayout có chế độ hiển thị được đặt thành "Đã qua" theo mặc định, Tôi cần có chiều cao của chế độ xem này để thực hiện hoạt ảnh trượt khi nó hiển thị. Làm thế nào để tôi có được tổng chiều cao của trạng thái hiển thị, vì View.getHeight trả về số không khi bố cục không được gọi.getHeight cho Chế độ xem có khả năng hiển thị = đi

<LinearLayout 
    android:id="@+id/card_checkin_layout_termsconditionsconfirmation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="4dp" 
    android:gravity="center_horizontal" 
    android:background="#d0d0d0" 
    android:visibility="invisible" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/card_checkin_button_confirmdetails" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:background="@drawable/shape_checkin_buttons2" 
     android:text="> Confirm your details" 
     android:paddingLeft="8dp"    
     android:gravity="left|center_vertical" 
     android:textColor="@color/card_checkin_button_textcolor_blue" 
     /> 

    <Button 
     android:id="@+id/card_checkin_button_termsandconditions" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginBottom="8dp" 
     android:paddingLeft="8dp"    
     android:background="@drawable/shape_checkin_buttons2" 
     android:text="> Terms and Conditions" 
     android:gravity="left|center_vertical" 
     android:textColor="@color/card_checkin_button_textcolor_blue" 
     /> 

</LinearLayout> 
+5

Set nó cho 'View.VISIBLE' ban đầu. Sử dụng 'GlobalLayoutListener' để nhận một cuộc gọi lại khi bố cục được xây dựng, lấy chiều cao, sau đó đặt nó thành' View.GONE' và bố cục yêu cầu. – Simon

Trả lời

12

Ban đầu đặt chế độ hiển thị của chế độ xem là hiển thị hoặc ẩn để bắt đầu, để chiều cao được tính. Sau đó thay đổi khả năng hiển thị để biến mất.

FYI: removeGlobalOnLayoutListener() không được dùng nữa vì cấp API 16, nó được thay thế bằng removeOnGlobalLayoutListener().

Bạn có thể thử này:

// onCreate or onResume or onStart ... 
mView = findViewByID(R.id.someID); 
mView.getViewTreeObserver().addOnGlobalLayoutListener( 
    new OnGlobalLayoutListener(){ 

     @Override 
     public void onGlobalLayout() { 
      // gets called after layout has been done but before display 
      // so we can get the height then hide the view  

      mHeight = mView.getHeight(); 

      mView.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      mView.setVisibility(View.GONE); 
     } 

}); 
+4

Chỉ cần làm rõ, 'removeGlobalOnLayoutListener()' chỉ bị phản đối do lỗi đánh máy. Vẫn an toàn để sử dụng (hiện tại) trên API> = 16, nhưng tôi khuyên bạn nên tạo phương thức tiện ích tĩnh gọi OnGlobal cho API 16 trở lên và GlobalOn cho các cấp thấp hơn. – kcoppock

+0

cách này không hoạt động – Carl

+0

Lấy từ https://stackoverflow.com/a/10134260/5364383 –

Các vấn đề liên quan