【问题标题】:put three controls in a row at the bottom of the screen在屏幕底部连续放置三个控件
【发布时间】:2013-11-24 01:04:53
【问题描述】:

如何指定布局以在屏幕底部连续放置三个控件?我想要一个左对齐的 TextView 和一个居中的组中的两个按钮。 如果没有“间隔”视图,所有三个控件都在左侧对齐。随着代码的发布,我接近了我想要的。有没有办法用按钮使 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>

我尝试了下面给出的两种解决方案。我想要的也没有。 一个定位三个控件:左中和右。另一个将最左边的控件分布在整个屏幕上,并将另外两个控件卡在右侧。

第一张图是我的尝试。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:
    // 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>
    

    【讨论】:

    • 未按要求执行。见上图。
    【解决方案2】:

    试试这个..

    <?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>
    

    【讨论】:

    • 你能说一下你改变了什么以及为什么改变它吗?
    • @NormR 我改变了linearLayout1 可见的东西。我没有设置特定的宽度,只是改变了要显示的重量。
    • @NormR 检查我的编辑在那个按钮哈希两个权重所以只有我编辑了检查。
    • 我删除了 weight=0 行并使用 weight=1 行进行了测试。结果如上图所示。它没有达到我想要的效果。
    • @NormR 从您需要的图片中提取。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多