簡體   English   中英

Android TableLayout和水平重力

[英]Android TableLayout and horizontal gravity

我有一個TableLayout僅包含一行和一個單元格。 我希望單元格的內容水平居中。 我嘗試了兩種方法:

第一種方法,我在TableRow元素上設置了屬性android:gravity =“ center_horizo​​ntal” ,它可以按預期工作:

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TableRow android:gravity="center_horizontal">
            <Button
                android:id="@+id/MyButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"            
                android:text="@string/Hello" />
        </TableRow>
    </TableLayout>

第二種方法,我直接在單元格內容(此處的按鈕)上設置了屬性android:layout_gravity =“ center_horizo​​ntal” ,但沒有將按鈕居中:

<?xml version="1.0" encoding="utf-8"?>
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TableRow>
                <Button
                    android:id="@+id/MyButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"            
                    android:layout_gravity="center_horizontal"
                    android:text="@string/Hello" />
            </TableRow>
        </TableLayout>

有人可以解釋一下為什么第二種方法不起作用嗎?

謝謝

里安娜

android:gravity引用視圖中的子內容。 在您的情況下,這會影響表格單元格的內容。

android:layout_gravity表示視圖在其父級中的位置。 在這種情況下,表格中單元格的位置,而不是單元格內容。 單元格內容不會受到影響。

有關其他示例和更多說明,請參見此類似問題: Android上的Gravity和layout_gravity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM