2012-09-27 57 views
2

我正在嘗試編輯佈局文件,以便兩個按鈕並排排列,而不是一個在另一個之上。以下是從佈局文件代碼:編輯佈局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="225dp" 
android:layout_height="wrap_content" 
android:background="@color/dark_yellow" 
android:orientation="vertical" 
android:padding="12dip" > 

<TextView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center_horizontal" 
android:paddingBottom="12dip" 
android:text="@string/dialog_text" 
android:textColor="@color/light_yellow" 
android:textSize="@dimen/font_medium" /> 

<Button 
android:id="@+id/btn_go_to_new_App" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:onClick="Go" 
android:text="@string/btn_go" /> 

<Button 
android:id="@+id/btn_stay" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:onClick="stay" 
android:text="@string/btn_stay_at_lifecycle" /> 

</LinearLayout> 

截至目前,btn_go正在出現上述btn_stay_at_lifecycle。我需要做什麼才能使btn_stay_at_lifecycle出現在btw_go的右側?

謝謝!

+0

現在選擇的答案:) – 2012-09-27 06:57:22

回答

1

嘗試

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="225dp" 
android:layout_height="wrap_content" 
android:background="@color/dark_yellow" 
android:orientation="vertical" 
android:padding="12dip" > 

<TextView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center_horizontal" 
android:paddingBottom="12dip" 
android:text="@string/dialog_text" 
android:textColor="@color/light_yellow" 
android:textSize="@dimen/font_medium" /> 

<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 

     <Button 
    android:id="@+id/btn_go_to_new_App" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight=".5" 
    android:onClick="Go" 
    android:text="@string/btn_go" /> 

    <Button 
    android:id="@+id/btn_stay" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight=".5" 
    android:onClick="stay" 
    android:text="@string/btn_stay_at_lifecycle" /> 

</LinearLayout> 
    </LinearLayout> 
3

您的LinearLayout被定義爲vertical,因此您添加的每個項目都將被垂直組織。

如果您需要一些物品並排放置在LinearLayout與屬性android:orientation="horizontal"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="225dp" 
    android:layout_height="wrap_content" 
    android:background="@color/dark_yellow" 
    android:orientation="vertical" 
    android:padding="12dip" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:paddingBottom="12dip" 
     android:text="@string/dialog_text" 
     android:textColor="@color/light_yellow" 
     android:textSize="@dimen/font_medium" /> 

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

     <Button 
      android:id="@+id/btn_go_to_new_App" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:onClick="Go" 
      android:text="@string/btn_go" /> 

     <Button 
      android:id="@+id/btn_stay" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:onClick="stay" 
      android:text="@string/btn_stay_at_lifecycle" /> 

    </LinearLayout> 

</LinearLayout> 

The default value of orientation of a LinearLayout is "horizontal"。因此,即使您沒有在內部LinearLayout中指定android:orientation,它也會並排排列其內容。

3

使用下列內容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="225dp" 
android:layout_height="wrap_content" 
android:background="@color/dark_yellow" 
android:orientation="vertical" 
android:padding="12dip" > 

<TextView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center_horizontal" 
android:paddingBottom="12dip" 
android:text="@string/dialog_text" 
android:textColor="@color/light_yellow" 
android:textSize="@dimen/font_medium" /> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" > 
<Button 
android:id="@+id/btn_go_to_new_App" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:onClick="Go" 
android:text="@string/btn_go" /> 

<Button 
android:id="@+id/btn_stay" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:onClick="stay" 
android:text="@string/btn_stay_at_lifecycle" /> 
</LinearLayout> 
</LinearLayout> 
3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="225dp" 
android:layout_height="wrap_content" 
android:background="@color/dark_yellow" 
android:orientation="vertical" 
android:padding="12dip" > 

<TextView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center_horizontal" 
android:paddingBottom="12dip" 
android:text="@string/dialog_text" 
android:textColor="@color/light_yellow" 
android:textSize="@dimen/font_medium" /> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/btn_go_to_new_App" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:onClick="Go" 
     android:text="@string/btn_go" /> 

    <Button 
     android:id="@+id/btn_stay" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:onClick="stay" 
     android:text="@string/btn_stay_at_lifecycle" /> 
</LinearLayout> 

</LinearLayout> 
3

試試這個

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="225dp" 
    android:layout_height="wrap_content" 
    android:background="@color/dark_yellow" 
    android:orientation="vertical" 
    android:padding="12dip" > 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:paddingBottom="12dip" 
    android:text="@string/dialog_text" 
    android:textColor="@color/light_yellow" 
    android:textSize="@dimen/font_medium" /> 

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

    <Button 
    android:id="@+id/btn_go_to_new_App" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:onClick="Go" 
    android:text="@string/btn_go" /> 

    <Button 
    android:id="@+id/btn_stay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:onClick="stay" 
    android:text="@string/btn_stay_at_lifecycle" /> 
    </LinearLayout> 
    </LinearLayout>