layout_gravity в LinearLayout - PullRequest
       55

layout_gravity в LinearLayout

18 голосов
/ 24 января 2011

Это мой макет:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="5dip" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textColor="#ffffff" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/nazajGumb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/roaming_backbtn" >
        </Button>

        <Button
            android:id="@+id/homeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/roaming_homebtn" >
        </Button>
    </LinearLayout>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dip"
        android:prompt="@string/roaming_spinnerPrompt" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Random text"
        android:textColor="#ffffcc" />

    <Button
        android:id="@+id/testBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="test" >
    </Button>
</LinearLayout>

Положение ImageView и TextView в LinearLayout2 и расположение кнопок в LinearLayout3 не работает (используется гравитация макета).

Что мне здесь не хватает?

Ответы [ 2 ]

65 голосов
/ 24 января 2011

Это не то, как android:layout_gravity работает.Оба параметра, left и center_horizontal, работают только тогда, когда android:orientation равен vertical.Чтобы добиться того, что вы хотите, лучше использовать RelativeLayout:

  <RelativeLayout
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

        <ImageView   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dip"/>

        <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"  
        android:layout_centerHorizontal="true"
        android:textColor="#ffffff"/>
  </RelativeLayout>    
0 голосов
/ 24 января 2011
android:layout_marginLeft="5dip"

должно быть (dp not dip)

android:layout_marginLeft="5dp"
...