我一直在嘗試創建一個簡單的佈局。這是一個帶有下面TextView的3x3網格。 我使用TableLayout來設置ImageButtons和TextView(TextView具有layout_span = 3)。 問題是在橫向模式下我看不到TextView。Android:指定TableRows的大小
我試着把一些權重放到TableRows中,但是這樣可以修復橫向模式下創建其他問題的問題:在縱向模式下,按鈕之間有巨大的空間。
我想創造的是一個蜱粘釘遊戲,所以我想按鈕是緊湊的。
我該如何獲得這樣的效果?我希望Textview能夠保留所有的垂直空間,並且我希望即使在橫向模式下它也是可見的。
任何解決方案?
這是XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:stretchColumns="*">
<TableRow android:layout_margin="2dip"
android:background="#000000">
<ImageButton android:id="@+id/z_z" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
<ImageButton android:id="@+id/z_o" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
<ImageButton android:id="@+id/z_t" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
</TableRow>
<TableRow android:layout_margin="2dip"
android:background="#000000">
<ImageButton android:id="@+id/o_z" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
<ImageButton android:id="@+id/o_o" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
<ImageButton android:id="@+id/o_t" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
</TableRow>
<TableRow android:layout_margin="2dip"
android:background="#000000">
<ImageButton android:id="@+id/t_z" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
<ImageButton android:id="@+id/t_o" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
<ImageButton android:id="@+id/t_t" android:layout_margin="3dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/empty"
android:background="#ffffff"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/message_txt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/how_to_play"
android:textSize="@dimen/message_size"
android:layout_span="3"/>
</TableRow>
</TableLayout>
編輯: 我明白瞭如何使用權,但我認爲這實際上並沒有解決我的問題,因爲在不同屏幕尺寸的格柵依然是畸形。 我真正想要做的是在佈局代碼中使用屏幕大小的一種方法。 以這種方式,我可以使用適合它的權重。
也許我可以爲每個屏幕維度指定一個不同的佈局(我讀了類似於前一天或前兩天的內容),但這會有點無聊。而且如果我改變佈局,我還會有大量文件需要更新。
也許有一種方法可以將TableLayout高度設置爲wrap_content,同時將TextView高度設置爲fill_parent,將所有可能的空間顯示爲網格? 這將真正解決我的問題,每個屏幕尺寸。
行動!我認爲這還不是解決方案,因爲我注意到在不同的屏幕尺寸下,即使使用重量,它也可能會再次發生。 有沒有辦法告訴TextView採取所有的垂直空間呢?也許你可以使用weightSum來做到這一點,但我做不到。 – Bakuriu 2012-02-27 19:52:36
from開發者網站re:權重「例如,如果有三個文本框,其中兩個聲明權重爲1,而另一個沒有權重(0),則第三個沒有權重的文本框不會增長,並且會只佔用其內容所需的面積,另外兩個將平均擴大,以便在測量完所有三個盒子後填滿剩餘空間。「因此,給textrow的權重爲1的文本框,並將其他默認值設置爲0,textview行應該展開以填充可用空間。 [more here](http://developer.android.com/guide/topics/ui/layout-objects.html) – Barak 2012-02-28 15:40:26