8

Mam LinearLayout, którego widoczność jest domyślnie ustawiona na "Gone", Muszę uzyskać wysokość tego widoku, aby zrobić animację wysuwaną, kiedy będzie widoczna. Jak uzyskać łączną wysokość widocznego stanu, ponieważ View.getHeight zwraca zero, gdy układ nie jest wywoływany.getHeight for View, który ma widoczność = zniknął

<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

ustaw go na 'View.VISIBLE' początkowo. Użyj "GlobalLayoutListener", aby odebrać wywołanie zwrotne, gdy układ zostanie zbudowany, uzyskać wysokość, a następnie ustawić go na "View.GONE" i układ żądania. – Simon

Odpowiedz

12

Początkowo ustaw widoczność widoku jako widoczną lub niewidoczną na początku, aby wysokość została obliczona. Później zmień widoczność, aby zniknęła.

FYI: removeGlobalOnLayoutListener() jest przestarzałe od poziomu interfejsu API 16, jest zastąpione przez removeOnGlobalLayoutListener().

Można spróbować to:

// 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

Aby wyjaśnić, 'removeGlobalOnLayoutListener()' jest tylko przestarzałe z powodu literówki. Wciąż jest bezpieczny w użyciu (na razie) w interfejsach API> = 16, ale polecam wykonanie statycznej metody narzędziowej, która wywoła OnGlobal dla API 16 i nowszego, a GlobalOn dla niższych poziomów. – kcoppock

+0

w ten sposób nie działa – Carl

+0

Zaczerpnięte z https://stackoverflow.com/a/10134260/5364383 –

Powiązane problemy