2014-02-12 22 views
0

我有這個layout.xml,我更改了按鈕的「layout_gravity」屬性,但無論我分配哪個值,兩個按鈕都放置在linearlayout容器的左側。我想他們是居中對齊(註釋是教師的原因,但我把他們以防萬一,他們可能是問題):按鈕並不朝向線性佈局的中心

<LinearLayout 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<!--root element must specify the Android resource XML namespace --> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="24dp" 
    android:text="@string/question_text"/> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/true_button" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/false_button" 
     android:layout_gravity="right" /> 
    <!--The XML elements :: <string name="false_button">False</string> 
     will have to be defined in strings.xml--> 

</LinearLayout> 

+0

你應該接受你發現的有用的任何答案,這樣其他的Google用戶也得到它的好處.. :) –

回答

1

嘗試以下佈局。我編輯android:gravity="center_horizontal"到第二個LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <!-- root element must specify the Android resource XML namespace --> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="24dp" 
     android:text="Question" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="True" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:text="False" /> 
    </LinearLayout> 

</LinearLayout> 

說明

首先LinearLayoutgravity=center。對。它將集中子女。絕對正確。但第二個LinearLayout也居中,但它又是一個持有人。所以它有它自己的屬性。雖然第二個佈局的父級佈局具有gravity=center,,但它僅適用於其直接子女。不是孩子的孩子。因此,您必須再次聲明任何其他屬性才能適用於其子項。

+0

不知道爲什麼這個工程,但它確實。我的想法是,父母的重力屬性不應該影響孩子的嚴重性(在這種情況下)。但現在我想到了......你能否提供一個解釋它爲什麼有效的解釋?謝謝 –

+0

@MindaugasBernatavičius當然。我編輯了我的答案。請查閱。如果您發現它有幫助,請接受答案:) –

+0

從解釋看來,這些屬性是由直接父母的直接子女繼承的。我認爲這些屬性適用於聲明它們的元素,而不適用於兒童和父母。這很混亂。你能否澄清我是否正確? –

1

只需在您的內部LinearLayout中將android:layout_width="match_parent"更改爲android:layout_width="wrap_content"即可。

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="24dp" 
     android:text="@string/question_text"/> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/false_button" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/true_button" 
      android:layout_gravity="right" /> 

    </LinearLayout> 
</LinearLayout> 
+0

再說一遍,如果你提供了一個簡短的解釋,而不僅僅是一個快速的解釋,爲什麼這會起作用,這將是非常有幫助的。謝謝。 –

0

您可以管理這個使用安卓layout_weight如下...

<!-- root element must specify the Android resource XML namespace --> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="24dp" 
    android:text="@string/question_text" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/true_button" /> 

    <Button 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     android:text="@string/false_button" /> 
    <!-- 
    The XML elements :: <string name="false_button">False</string> 
    will have to be defined in strings.xml 
    --> 

</LinearLayout>