What is wrong with this simple layout?

Im trying to define a LinearLayout that contains another LinearLayout that should always be displayed in the horizontal and vertical center. The ImageView should always be positioned vertically on the right side of the parent layout:

                                 A                                           B

I defined it as follows:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="60px">

     <LinearLayout android:id="@+id/footer" android:orientation="horizontal" android:layout_height="50px" android:paddingTop="5px" android:layout_width="wrap_content" android:background="@drawable/footer_border" android:paddingRight="5px" android:paddingLeft="5px" android:layout_gravity="center_horizontal">
        <ImageButton android:id="@+id/libraryButton" android:layout_height="wrap_content" android:src="@drawable/library_button" android:background="#000000" android:layout_width="wrap_content"/>
        <ImageButton android:id="@+id/bookButton" android:layout_height="wrap_content" android:src="@drawable/book_button" android:background="#000000" android:layout_width="wrap_content" android:paddingLeft="15px" android:paddingRight="15px"/>
        <ImageButton android:id="@+id/workspaceButton" android:layout_height="wrap_content" android:src="@drawable/workspace_button" android:background="#000000" android:layout_width="wrap_content"/>
     </LinearLayout>

     <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="right"
     android:id="@+id/loading">
     </ImageView>

     </LinearLayout> 

But unfornatuley it doesn’t work ... LinearLayout (A) and ImageView (B) are on the left side .... But I set gravity to the center and to the right? Why?

+3
source share
2 answers

Gravity in a LinearLayout with a horizontal orientation will only work on top, bottom and center_vertical ( here ).

, - RelativeLayout LinearLayout. - :

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="60px">

<LinearLayout android:id="@+id/footer" 
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:center_in_parent="true"
>
....
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loading"
android:align_parent_right="true">
</ImageView>

</RelativeLayout> 

, . , .

+1

android:layout_alignParentRight

<ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="right"
 android:id="@+id/loading"
 android:layout_alignParentRight="true">
</ImageView>

, ...

0

Source: https://habr.com/ru/post/1744152/


All Articles