问题
I have been trying to use layout_gravity
in a horizontal linear layout
. But it doesn't seem to work. I don't want to use a relative layout
or any other layout. I want to know why it is not working with linear layout
.I have earlier used layout_gravity
in vertical
linear layouts and it has worked in the way I expected.
<LinearLayout
android:id="@+id/shata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:text="shata1"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/map_imageview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="shata2"/>
</LinearLayout>
As you can see the the linear layout has a orientation=horizontal
and width=match_parent
(it is just inside the root layout). But both the TextViews are displayed sticking to the left of the LinearLayout
even though I have given their layout_gravity=center
and layout_gravity=right
respectively.
回答1:
Short answer: If you have a horizontal LinearLayout, then each child can only have layout_gravity
values of top
, bottom
, and center
.
The reason for this is because you've already told the LinearLayout
to align each child horizontally, left-to-right in the order you specify, which means that it only lets you specify the vertical alignment of each child in the layout_gravity
parameter, and the opposite applies when your LinearLayout
is vertical.
There's a good link explaining how the various gravities work here.
回答2:
As an addition to Matt Taylor's response, you can specify the size ratio of the elements inside the layout using the layout_weight
property. See this thread for more details.
The code above will split the horizontal space in three equals areas.
<LinearLayout
android:id="@+id/shata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="0"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="0"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
So in this example
- the first element looks like
layout_gravity="left"
- the second element looks like
layout_gravity="center"
- the third element looks like
layout_gravity="right"
回答3:
you can divide layout to 3 part then in right part put "shata2" in center part put "shata1" and left part put a transparent view.
<LinearLayout
android:id="@+id/shata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/title_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_weight="1"
android:text="shata1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#111" />
<TextView
android:id="@+id/map_imageview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:gravity="right"
android:text="shata2"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
but in your code when you use linear layout it just put views one after other and it doesnt care the layout gravity. hope it be useful .
回答4:
If you want to have your child view centered then you just need to give your linear layout gravity for the children. On your linear layout set the following:
android:gravity="center"
I hope I got your question right.
回答5:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
style="@style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Blankets & Carpets" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list_home_feature"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:layout_marginRight="10dp">
</ListView>
</LinearLayout>
回答6:
One way for creating three childs in horizontal linearlayout would be(tested):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="" />
来源:https://stackoverflow.com/questions/33076051/layout-gravity-not-working-in-horizontal-linear-layout