Java Android-静态和动态按钮重量之间的差异
我正在使用TableLayout,其中第一行是静态XML,第二行是在运行时添加的。两种布局在视觉上应该相同。然而,我得到了不同的结果:Java Android-静态和动态按钮重量之间的差异,java,android,layout,Java,Android,Layout,我正在使用TableLayout,其中第一行是静态XML,第二行是在运行时添加的。两种布局在视觉上应该相同。然而,我得到了不同的结果: ROW 1: [----A-----] [----------B----------] [-----C----] ROW 2: [-----A-----] [---------B---------] [-----C-----] 下面是第1行的XML: <Button android:id="@+id/stat_A" android:layout_wi
ROW 1: [----A-----] [----------B----------] [-----C----]
ROW 2: [-----A-----] [---------B---------] [-----C-----]
下面是第1行的XML:
<Button
android:id="@+id/stat_A"
android:layout_width="0dp"
android:layout_weight="1"
android:text="A" />
按钮DYN_A和DYN_C的“DYN_重量”为1,按钮DYN_B的“DYN_重量”为2
我的错在哪里
谢谢
编辑
完整的XML:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Tbl01"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<TableRow
android:id="@+id/row1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/stat_A"
android:layout_width="0dp"
android:layout_weight="1"
android:text="A" />
<Button
android:id="@+id/stat_B"
android:layout_width="0dp"
android:layout_weight="2"
android:text="B" />
<Button
android:id="@+id/stat_C"
android:layout_width="0dp"
android:layout_weight="1"
android:text="C" />
</TableRow>
<TableRow
android:id="@+id/row2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
发布您的xml代码。我注意到的第一件事是,在代码0dp!=MATCH_PARENT。您不一定需要按代码创建所有视图,只需膨胀一个xml文件并设置正确的值即可。@Mikel aslo填充标题dynamically@Steve-是否有一种通过LayoutParams为宽度定义0dp的方法?@Pragnani-您能详细说明一下吗?
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Tbl01"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<TableRow
android:id="@+id/row1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/stat_A"
android:layout_width="0dp"
android:layout_weight="1"
android:text="A" />
<Button
android:id="@+id/stat_B"
android:layout_width="0dp"
android:layout_weight="2"
android:text="B" />
<Button
android:id="@+id/stat_C"
android:layout_width="0dp"
android:layout_weight="1"
android:text="C" />
</TableRow>
<TableRow
android:id="@+id/row2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>