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

问题描述:

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

这是我做的,但我最终得到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> 

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> 

关于什么的FrameLayout是为去这里解释:http://developer.android.com/reference/android/widget/FrameLayout.html

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

+0

相对布局是迄今为止最昂贵的布局机制。来源,Google工程师:https://plus.google.com/+KirillGrouchnikov/posts/fFfm7jKn7q5 – 2014-01-02 18:53:21

+0

您的链接“文章”(更像是句子)正在讨论适配器中的布局。 OP不要求这是在适配器中。只是因为这些话来自圣神的口中,并不意味着没有更多的事情发生。 – 2014-01-02 21:25:56

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

此外,它是非常CPU密集!不这样做,并阅读:

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

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