我在設置TableLayout
中列的寬度時遇到問題。這個問題已經在前面討論過:
How to set Table width and number of Columns in TableLayout in Android
How to get a TableLayout with 2 columns of equal width
Set equal width of columns in table layout in Android
,但我沒有找到一個解決方案。下面的代碼的確將列設置爲相等的寬度,但似乎將第一列中的按鈕關閉。由於 第二列中的按鈕文本較寬,因此它以不利的方式顯示。設置TableLayout中的列寬度
所需的顯示:
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/menu1Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@color/darkgrey2"
android:contentDescription="@string/menu1"
android:onClick="click1"
android:padding="60dp"
android:text="hello"
android:textColor="@color/white"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:id="@+id/menu2Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@color/darkgrey2"
android:contentDescription="@string/menu1"
android:onClick="click1"
android:padding="60dp"
android:text="hellothere"
android:textColor="@color/white"
android:textSize="32sp"
android:textStyle="bold" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/menu3Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@color/darkgrey2"
android:contentDescription="@string/menu1"
android:onClick="click1"
android:padding="60dp"
android:text="hello"
android:textColor="@color/white"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:id="@+id/menu4Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@color/darkgrey2"
android:contentDescription="@string/menu1"
android:onClick="click1"
android:padding="60dp"
android:text="hello"
android:textColor="@color/white"
android:textSize="32sp"
android:textStyle="bold" />
</LinearLayout>
</TableRow>
編輯: 卸下邊距和填充結果:
EDIT2: 只去掉保證金:
settin會有什麼影響g'android:maxLines =「1」'在'Button'的佈局中? – nKn
我在「hellothere」按鈕中添加了建議的行,它確實將其限制爲一行,但結果是該按鈕讀作「helloth」。這種情況下的所有按鈕都是相同的寬度和高度,只適合「你好」 –
這可能是由按鈕的「padding」屬性產生的。由於四邊(左邊,右邊,頂部,底部)的位置有所不同,它會減少可以顯示文本的佈局,可能這是問題,文本會被包裹,因爲沒有足夠的空間來顯示邊距線。 '60dp'確實是一個大的空間減少,試着大幅度減少它,看看會發生什麼。 – nKn