为什么 TableRow 中的按钮在使用 XML 创建时未正确对齐,但以编程方式创建时却正确对齐?

Why isn't the Button in a TableRow aligned properly when created with XML, but it's aligned correctly when created programmatically?

当我在 Android Studio Design 选项卡中创建行时,当我通过 XML 创建它们时,行中的按钮似乎没有正确对齐。

但是,当我通过 inflation 以编程方式添加它们时,它们会正确对齐(请注意,带有键名的最后一行被复制并直接粘贴到 XML,因此我可以测试是否我的 table 设置有问题):

这是行的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/active_key_row"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5pt"
    android:layout_marginEnd="5pt"
    android:visibility="visible">

    <TextView
        android:id="@+id/active_key_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Key Name" />

    <Button
        android:id="@+id/active_key_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/remove_key" />
</TableRow>

这是 XML 的 table:

<TableLayout
    android:id="@+id/bt_keys_table"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/active_key_row"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5pt"
        android:layout_marginEnd="5pt"
        android:visibility="visible">

        <TextView
            android:id="@+id/active_key_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Key Name" />

        <Button
            android:id="@+id/active_key_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remove_key" />
    </TableRow>

    <TableRow
        android:id="@+id/empty_key_row"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5pt"
        android:layout_marginEnd="5pt"
        android:visibility="visible">


        <TextView
            android:id="@+id/empty_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />

        <Button
            android:id="@+id/add_key_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:text="@string/add_key" />
    </TableRow>

</TableLayout>

这是 inflation 的 Java 代码:

private void buildBtKeysTable() {

    mBtKeysTable = (TableLayout) findViewById(R.id.bt_keys_table);

    // Add the rows to the table
    for (int i = 0; i < DEFAULT_MAX_KEYS; i++) {
        // Inflate the row
        final TableRow row = (TableRow)
                getLayoutInflater().inflate(R.layout.active_keys_table_row, null);

        mBtKeysTable.addView(row, 0);

        Button button = (Button) row.findViewById(R.id.active_key_button);

        // Subscribe to the active buttons for bluetooth
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                removeBluetoothKey(row);
            }
        });
    }
}

也为按钮添加权重属性。如果不移除 textview 和按钮的重量,并尝试为按钮添加重力。

先生,那是因为您将 null 作为父 ViewGroup 参数传递给 inflate()。 这将导致所有 layout_* 属性被忽略,因为 inflater 不知道哪些属性对其将放置在其中的容器有效(即它不知道在视图上设置哪种 LayoutParams 类型)。

let we explore more ..

尝试替换这个

getLayoutInflater().inflate(R.layout.active_keys_table_row, null);
                            with         
getLayoutInflater().inflate(R.layout.active_keys_table_row, item, false);

并弄清楚发生了什么。