2009-08-12 45 views
3

我嘗試在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> 

回答

5

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

0

你也可以只使用一個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

2

可以使用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> 
相關問題