2013-11-23 47 views
0

如何指定一個佈局以將三個控件放在屏幕底部的一行中?我想要一個TextView左對齊和兩個按鈕在一個組中。 沒有「spacer」視圖,所有三個控件都在左邊對齊在一起。隨着代碼發佈,我接近我想要的。有沒有辦法將LinearLayout與按鈕居中?在屏幕底部連續放置三個控件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
android:orientation="vertical" 
android:layout_width="fill_parent"> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_above="@+id/linearLayout1" 
    android:choiceMode="multipleChoice" 
    android:singleLine="true" 
    android:textStyle="bold" > 
</ListView> 

<!-- Row of buttons to control the display of the holdings --> 
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" 
    android:background="@color/btnarea" 
    > 
    <!--  setText here disables "selection" of items ???? --> 
    <TextView 
     android:id="@+id/selectedWPs" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="#ae6633" 
     android:text="Selected: " 
     android:layout_gravity="left" 
     android:layout_weight="0" 
     android:gravity="center" 
     android:textSize="20sp" /> 

    <!-- spacer ???? why doesn't center_horizontal work??? --> 
    <View android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:background="@color/btnarea" /> 

    <!-- how to center following ??? --> 
    <LinearLayout 
     android:id="@+id/buttonArea" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="0" 
     > 

     <Button 
      android:id="@+id/quitBtn" 
      android:layout_width="match_parent" 
      android:layout_height="63dp" 
      android:background="#990000" 
      android:onClick="quitBtnClicked" 
      android:text="Quit" 
      android:textAlignment="center" /> 

     <!-- spacer --> 
     <View 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:background="@color/btnarea" /> 

     <Button 
      android:id="@+id/copyBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="0" 
      android:enabled="false" 
      android:background="#009900" 
      android:onClick="copyBtnClicked" 
      android:text="Copy" 
      android:textAlignment="center" /> 

    </LinearLayout> 
</LinearLayout> 

我試過如下兩種解決方案。我也沒有做到我想要的。 一個定位了三個控件:左中心和右。另一個將最左邊的控件分佈在整個屏幕上,並將其他兩個控件卡在右邊。

第一個圖像是我的嘗試。

This is my attempt

First layout

Second layout

回答

0

試試這個..

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/linearLayout1" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:choiceMode="multipleChoice" 
     android:singleLine="true" 
     android:textStyle="bold" > 
    </ListView> 

    <!-- Row of buttons to control the display of the holdings --> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="#969696" 
     android:orientation="horizontal" > 

     <!-- setText here disables "selection" of items ???? --> 

     <TextView 
      android:id="@+id/selectedWPs" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:layout_weight="0.25" 
      android:background="#ae6633" 
      android:gravity="center" 
      android:text="Selected: " 
      android:textSize="20sp" /> 

     <!-- spacer ???? why doesn't center_horizontal work??? --> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.25" 
      android:background="#a3a3a3" /> 

     <!-- how to center following ??? --> 

     <LinearLayout 
      android:id="@+id/buttonArea" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="0.5" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/quitBtn" 
       android:layout_width="0dp" 
       android:layout_height="63dp" 
       android:layout_weight="1" 
       android:background="#990000" 
       android:onClick="quitBtnClicked" 
       android:text="Quit" 
       android:textAlignment="center" /> 

      <!-- spacer --> 

      <View 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="#d5d5d5" /> 

      <Button 
       android:id="@+id/copyBtn" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:layout_weight="0" 
       android:background="#009900" 
       android:enabled="false" 
       android:onClick="copyBtnClicked" 
       android:text="Copy" 
       android:textAlignment="center" /> 
     </LinearLayout> 
    </LinearLayout> 

</RelativeLayout> 

編輯:

  <Button 
       android:id="@+id/copyBtn" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="#009900" 
       android:enabled="false" 
       android:onClick="copyBtnClicked" 
       android:text="Copy" 
       android:textAlignment="center" /> 

編輯2:

<LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="#969696" 
     android:orientation="horizontal" > 

     <!-- setText here disables "selection" of items ???? --> 

     <TextView 
      android:id="@+id/selectedWPs" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:layout_weight="0.15" 
      android:background="#ae6633" 
      android:gravity="center" 
      android:text="Selected: " 
      android:textSize="20sp" /> 

     <!-- spacer ???? why doesn't center_horizontal work??? --> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.15" 
      android:background="#a3a3a3" /> 

     <!-- how to center following ??? --> 

     <LinearLayout 
      android:id="@+id/buttonArea" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="0.70" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/quitBtn" 
       android:layout_width="0dp" 
       android:layout_height="63dp" 
       android:layout_weight="1" 
       android:background="#990000" 
       android:onClick="quitBtnClicked" 
       android:text="Quit" 
       android:textAlignment="center" /> 

      <!-- spacer --> 

      <View 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="#d5d5d5" /> 

      <Button 
       android:id="@+id/copyBtn" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:layout_weight="0" 
       android:background="#009900" 
       android:enabled="false" 
       android:onClick="copyBtnClicked" 
       android:text="Copy" 
       android:textAlignment="center" /> 

      <View 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="#d5d5d5" /> 
     </LinearLayout> 
    </LinearLayout> 
+0

你能說出你的改變,爲什麼要換呢? – NormR

+0

@NormR我改變了'linearLayout1'可見的東西。我沒有設置特定的寬度只改變了顯示的重量。 – Hariharan

+0

那個有一個語法錯誤:兩個weight =字段。 – NormR

0
// try this layout and modify as per your requirement and if any problem then let me know ... 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_width="fill_parent"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:choiceMode="multipleChoice" 
     android:singleLine="true" 
     android:textStyle="bold" > 
    </ListView> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@color/btnarea"> 
     <TextView 
      android:id="@+id/selectedWPs" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:background="#ae6633" 
      android:text="Selected: " 
      android:gravity="center" 
      android:textSize="20sp" /> 

     <LinearLayout 
      android:id="@+id/buttonArea" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_marginLeft="5dp" 
      android:orientation="horizontal"> 

      <Button 
       android:id="@+id/quitBtn" 
       android:layout_width="wrap_content" 
       android:layout_height="63dp" 
       android:background="#990000" 
       android:onClick="quitBtnClicked" 
       android:text="Quit" 
       android:textAlignment="center" /> 

      <Button 
       android:id="@+id/copyBtn" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:enabled="false" 
       android:background="#009900" 
       android:onClick="copyBtnClicked" 
       android:layout_marginLeft="5dp" 
       android:text="Copy" 
       android:textAlignment="center" /> 

     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 
+0

沒有按要求做。看到上面的圖片。 – NormR

相關問題