如何在 android layout.xml 文件中创建 2 行按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1264465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:52:32  来源:igfitidea点击:

How can I create 2 rows of buttons in android layout.xml file

android

提问by lucius

I try to create 2 rows of buttons in android layout.xml file. The first row is left-aligned, the second row is center-aligned.

我尝试在 android layout.xml 文件中创建 2 行按钮。第一行左对齐,第二行居中对齐。

Here is what I did, but I end up getting 1 row of buttons. Can you please tell me what am I doing wrong?

这是我所做的,但我最终得到了 1 排按钮。你能告诉我我做错了什么吗?

enter code here
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
android:layout_height="fill_parent">
 <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_gravity="bottom|left"
        >
         <Button 
          android:id="@+id/btn1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
        </LinearLayout>
     <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_gravity="bottom|center"
        >
         <Button 
          android:id="@+id/btn2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
       <Button 
          android:id="@+id/btn3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
        </LinearLayout>
</FrameLayout>
</pre>

回答by reflog

FrameLayout is wrong for this task. Use LinearLayout like this:

FrameLayout 对于此任务是错误的。像这样使用 LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="fill_parent">
 <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_gravity="bottom|left"
        >
         <Button
          android:id="@+id/btn1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
        </LinearLayout>
     <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_gravity="bottom|center"
        >
         <Button
          android:id="@+id/btn2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
       <Button
          android:id="@+id/btn3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
        </LinearLayout>
</LinearLayout>

for explanation on what FrameLayout is for go here: http://developer.android.com/reference/android/widget/FrameLayout.html

有关 FrameLayout 的解释,请访问:http: //developer.android.com/reference/android/widget/FrameLayout.html

回答by davidcesarino

I would recommend against nesting LinearLayouts like that. It's not productive, very inefficient and hinders the ability of Android layouts to expand in some cases and UI changes.

我建议不要像这样嵌套 LinearLayouts。它没有生产力,非常低效,并且在某些情况下阻碍了 Android 布局扩展和 UI 更改的能力。

And besides, it's very CPU intensive! Don't do it and read this:

此外,它非常占用 CPU!不要这样做并阅读以下内容:

http://developer.android.com/training/improving-layouts/optimizing-layout.html

http://developer.android.com/training/improving-layouts/optimizing-layout.html

回答by Ramesh Yankati

You can use Tablelayout for displaying button in the form of rows.

您可以使用 Tablelayout 以行的形式显示按钮。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:id="@+id/root"
      android:stretchColumns="*"
      android:background="#000000">
  <TableRow android:layout_margin="0dip"
         android:id="@+id/first_row">
    <Button android:id="@+id/button01" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
    <Button android:id="@+id/button02" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
    <Button android:id="@+id/button03" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
    <Button android:id="@+id/button04" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
    <Button android:id="@+id/button05" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
  </TableRow>
</TableLayout>

回答by Erik B

You could also use just one RelativeLayout instead of all of your layouts. I've read many reports that RelativeLayout uses less resources than LinearLayout.

您也可以只使用一个 RelativeLayout 而不是所有的布局。我读过很多报告,RelativeLayout 使用的资源比 LinearLayout 少。