【问题标题】:Linear Layout with two buttons side by side - android具有并排两个按钮的线性布局 - android
【发布时间】:2013-01-31 06:57:49
【问题描述】:

我在 layout.xml 下面有 3 个按钮,它们出现在彼此下方...

                <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="10dip" >

                    <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <Button
                        android:id="@+id/btn_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Car" />

                    <Button
                        android:id="@+id/btn_2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Vehicle" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/btn_3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Bike" />
                </LinearLayout>
            </LinearLayout>

我希望前两个按钮并排放置(btn_1 和 btn_2)。谁能给我一个关于如何做到这一点的提示???

非常感谢

【问题讨论】:

标签: android android-linearlayout


【解决方案1】:

只需将布局的android:orientation="vertical" 更改为android:orientation="horizontal",一切都会正常工作

【讨论】:

    【解决方案2】:

    最好的方法是制作布局,然后像这段代码一样放置你的按钮

    <TableRow 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
     <Button 
        android:id="@+id/Button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="@string/Home1"/>  
    
     <Button 
        android:id="@+id/Button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"     
        android:text="@string/NextL"/>
    

    在这种形状中,您在同一行中有两个按钮非常简单:D

    【讨论】:

    • 好像你放弃了结束标签?此外,根据docs,“应始终将 TableRow 用作 TableLayout 的子项”。
    • 是的,我忘记了这些,不,它们可以在大多数布局中使用不是义务,我在 LinearLayout 中使用它们并且效果很好
    • 这就是为什么文档只提到应该而不是必须。但正如文档所反对的那样,我也不推荐它,以防新的 Android 版本在那里出现问题。
    【解决方案3】:

    将线性布局方向垂直更改为水平。然后将两个按钮的权重设置为 1 或 2。如您所愿。按钮将平均排列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2015-08-11
      相关资源
      最近更新 更多