2012-04-26 174 views
0

我可能在這裏錯過了一些明顯的東西,但我似乎無法弄清楚這一點。我正在尋找像1,5或6這樣的結果,一行3個均勻分佈的按鈕,它們填滿了屏幕的寬度。但不幸的是,這些方法中的每一種似乎都有問題。 1給我一個警告:「這個TableRow佈局或它的TableLayout父項可能是無用的」,第5和第6章給出「嵌套權重對性能不利」。所以基本上我想知道做這件事的最好方法是什麼。我應該忽略一號警告或者我可以讓他們與第一行後,短短的TableRow或TableLayout拉伸填充行水平android xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > 

<!-- No.1 --> 
<TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" > 
    <TableRow android:id="@+id/tbRow0" android:layout_width="wrap_content" android:layout_height="wrap_content" > 
     <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num1" /> 
     <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num2" /> 
     <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num3" /> 
    </TableRow> 
</TableLayout> 

<!-- No.2 --> 
<TableRow android:id="@+id/tbRow1" android:layout_width="fill_parent" android:layout_height="wrap_content" > 
    <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num1" /> 
    <Button android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num2" /> 
    <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num3" /> 
</TableRow> 

<!-- No.3 --> 
<TableRow android:id="@+id/tbRow2" android:layout_width="fill_parent" android:layout_height="wrap_content" > 
    <Button android:id="@+id/btn7" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/num1" /> 
    <Button android:id="@+id/btn8" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/num2" /> 
    <Button android:id="@+id/btn9" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/num3" /> 
</TableRow> 

<!-- No.4 --> 
<TableRow android:id="@+id/tbRow3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" > 
    <Button android:id="@+id/btn10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num1" /> 
    <Button android:id="@+id/btn11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num2" /> 
    <Button android:id="@+id/btn12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num3" /> 
</TableRow> 

<!-- No.5 --> 
<TableRow android:id="@+id/tbRow4" android:layout_width="fill_parent" android:layout_height="wrap_content" > 
    <Button android:id="@+id/btn13" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num1" /> 
    <Button android:id="@+id/btn14" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num2" /> 
    <Button android:id="@+id/btn15" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num3" /> 
</TableRow> 

<!-- No.6 --> 
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > 
    <Button android:id="@+id/btn16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num1" /> 
    <Button android:id="@+id/btn17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num2" /> 
    <Button android:id="@+id/btn18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num3" /> 
</LinearLayout> 

<!-- No.7 --> 
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > 
    <Button android:id="@+id/btn19" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num1" /> 
    <Button android:id="@+id/btn20" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num2" /> 
    <Button android:id="@+id/btn21" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num3" /> 
</LinearLayout> 

</LinearLayout> 

回答

0

在XML文件中您正在關閉tablelayout但即使你是在2使用錶行正常顯示,5行可能是一個問題。

如果你只需要它一個然後更好地有linearlayout,因爲性能不壞,因爲你有一個小的XML文件,這是好的。

+0

耶,我應該更清楚。我只想要一行。我會使用LinearLayout,但「android:stretchColumns =」*「」不會爲此工作,所以有沒有類似的屬性來使用按鈕來均勻分佈按鈕? – 2012-04-26 04:38:58

+0

編輯我的答案。告訴我,如果這是你想要的! – Chopin 2012-04-26 15:40:44

1

我覺得5是正確的,再加上我沒有得到嵌套的權重警告。

也許它與Agarwal指出的有關。

編輯:對不起,我雖然5使用LinearLayout。我的意思是:

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > 
    <Button android:id="@+id/btn13" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num1" /> 
    <Button android:id="@+id/btn14" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num2" /> 
    <Button android:id="@+id/btn15" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num3" /> 
</LinearLayout> 
+0

是的,當我在另一個LinearLayout中爲ScrollView使用它時,我只得到了這個警告。我想我會忽略它,因爲我真的不知道它會受到多大的傷害。 – 2012-04-26 21:29:23