当我在Android Studio Design选项卡中创建行时,当我通过XML创建行时,行中的按钮似乎没有正确对齐。
但是,当我通过膨胀以编程方式添加它们时,它们会正确对齐(请注意,带有Key Name的最后一行被复制并直接粘贴到XML中,因此我可以测试我的表设置是否有问题):
以下是该行的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:
<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>
以下是通货膨胀的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);
}
});
}
}
答案 0 :(得分:1)
先生,这是因为您将null作为父ViewGroup参数传递给inflate()。 这将导致忽略所有layout_ *属性,因为inflater不知道哪个属性对于放置它的容器有效(即它不知道在View上设置哪个LayoutParams类型)。
让我们探索更多......
尝试替换此
getLayoutInflater().inflate(R.layout.active_keys_table_row, null);
with
getLayoutInflater().inflate(R.layout.active_keys_table_row, item, false);
并弄清楚发生了什么。
答案 1 :(得分:0)
也为按钮添加权重属性。如果没有从文本视图和按钮中删除重量,并尝试将重力添加到按钮的右侧。