【发布时间】:2016-10-27 07:50:01
【问题描述】:
当我在 Android Studio 设计选项卡中创建行时,当我通过 XML 创建行中的按钮时,它们似乎没有正确对齐。
但是,当我通过膨胀以编程方式添加它们时,它们会正确对齐(请注意,带有键名的最后一行被复制并直接粘贴到 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);
}
});
}
}
【问题讨论】:
-
去掉 android:layout_marginEnd="5pt" 再检查一遍。
-
@Redhey 有效,但我在 TableRow xml 文件上也设置了边距,以编程方式创建时似乎没有问题。这真的很奇怪:/
-
这是因为您将 null 作为父 ViewGroup 参数传递给 inflate()。这将导致所有 layout_* 属性被忽略,因为充气器不知道哪些属性对于将要放置的容器有效(即它不知道要在视图上设置哪种 LayoutParams 类型)。
-
尝试替换这个 getLayoutInflater().inflate(R.layout.active_keys_table_row, null);与 View child = getLayoutInflater().inflate(R.layout.active_keys_table_row, item, false);并弄清楚发生了什么。
-
您的卫生间,随时随地 :)
标签: java android xml android-inflate