在我的 Activity
的 onCreate
方法中,我知道 col0
中我想要的 Buttons
数量。在以下示例中,它是四个按钮
。然后我的 TextView
v0
将 android:layout_weight
设置为 按钮数量减一
(这是因为 >v1
应与所有按钮
具有相同的高度。如果可以概括为动态 Java 代码以某种方式生成适当的布局,而不是为每个可能数量的按钮提供一个自己的 xml 文件,那就更好了。这怎么可能?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/col0"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical">
<Button
android:id="@+id/b0"
style="@style/MyButton" />
<Button
android:id="@+id/b1"
style="@style/MyButton" />
<Button
android:id="@+id/b2"
style="@style/MyButton" />
<Button
android:id="@+id/b3"
style="@style/MyButton" />
</LinearLayout>
<LinearLayout
android:id="@+id/col1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="6"
android:baselineAligned="false"
android:orientation="vertical">
<TextView
android:id="@+id/v0"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3" />
<TextView
android:id="@+id/v1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
最佳答案
首先准备所有可能的 id,这里最多 10 个按钮(将其放入 res/values
中):
ids.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<item type="id" name="b0" />
<item type="id" name="b1" />
<item type="id" name="b2" />
<item type="id" name="b3" />
<item type="id" name="b4" />
<item type="id" name="b5" />
<item type="id" name="b6" />
<item type="id" name="b7" />
<item type="id" name="b8" />
<item type="id" name="b9" />
</resources>
如果您使用的 API >= 17,则可以避免使用此文件并使用 View.generateViewId()
如果不需要id,请跳过以上部分
然后,这是您的主要布局:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/col0"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/col1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="6"
android:baselineAligned="false"
android:orientation="vertical">
<TextView
android:id="@+id/v0" />
<TextView
android:id="@+id/v1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
然后,使用代码生成布局:
setContentView(R.layout.main);
int buttonsNumber = 5; // Put here your number of buttons
LinearLayout col0 = (LinearLayout) findViewById(R.id.col0);
for(int i = 0; i < buttonsNumber; i++)
{
try
{
Button newButton = new Button(this);
newButton.setId(R.id.class.getField("b"+i).getInt(null));
// Since API Level 17, you can also use View.generateViewId()
newButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
col0.addView(newButton);
}
catch (Exception e)
{
// Unknown button id !
// We skip it
}
}
TextView v0 = (TextView) findViewById(R.id.v0);
v0.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.match_parent, 0, buttonsNumber - 1));
关于java - LinearLayout 中的动态按钮数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680275/