java - LinearLayout 中的动态按钮数量

标签 java android xml android-layout layout

在我的 ActivityonCreate 方法中,我知道 col0 中我想要的 Buttons 数量。在以下示例中,它是四个按钮。然后我的 TextView v0android: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/

相关文章:

java - 如何在问答游戏中保持得分?

android - 从 SQLite 数据库中获取某个点周围的所有项目

java - spring jdbctemplate中查询返回 boolean 值?

java - 接口(interface)中的静态常量默认为 0 —— 为什么会这样?

java - 使用 Spring Security 时获取当前登录的用户名

android - Sencha touch + phonegap。 Dataview 在 android 4.0 上呈现

java - jsp中的XSS攻击

javascript - 将 Xml 文件链接到 HTML

Java Android异常,错误内容必须有一个id为android.R.id.list的ListView。机器人 :id="@android:id/list Does Not fix Issue

java - 在 XML 文件中搜索字符串