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

如何在android layout.xml文件中创建两行按钮,android,Android,我尝试在android layout.xml文件中创建两行按钮。 第一行左对齐,第二行居中对齐 这是我做的,但我最终得到了一排按钮。你能告诉我我做错了什么吗 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.xml文件中创建两行按钮。 第一行左对齐,第二行居中对齐

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

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>
在此处输入代码

此任务的FrameLayout错误。 使用线性布局,如下所示:

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


有关FrameLayout的说明,请转到此处:

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

我建议不要这样嵌套LinearLayout。它效率不高,效率很低,并且在某些情况下妨碍了Android布局的扩展能力和UI更改

而且,它非常占用CPU!不要这样做,请阅读以下内容:


您可以使用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>

相对布局是迄今为止最昂贵的布局机制。来源,一位谷歌工程师:你链接的“文章”(更像一句话)是关于适配器内的布局。OP不要求将其放在适配器中。仅仅因为这些话出自圣神之口,并不意味着没有更多的事情发生。