【问题标题】:Dynamic number of buttons in LinearLayoutLinearLayout 中的动态按钮数
【发布时间】:2014-12-28 20:23:35
【问题描述】:

在我的ActivityonCreate 方法中,我知道我想要在col0 中的Buttons 的数量。在以下示例中,它是四个 Buttons。然后我的TextViewv0android:layout_weight 设置为number of buttons minus one(这是因为v1 应该与所有Buttons 具有相同的高度)。与其为每个可能的按钮数量提供一个自己的 xml 文件,不如将其概括为更好的方法,以便动态 java 代码以某种方式生成适当的布局。这怎么可能?

<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>

【问题讨论】:

  • 您不能以编程方式编辑权重吗?
  • 我认为问题出在Buttons的生成部分。
  • 为什么我的问题被否决了?似乎没有人有合适的解决方案。

标签: java android xml android-layout layout


【解决方案1】:

首先准备所有可能的 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));

【讨论】:

  • 感谢您的回答。在@style/MyButton 中显示&lt;item name="android:layout_height"&gt;0dip&lt;/item&gt;&lt;item name="android:layout_weight"&gt;1&lt;/item&gt;。这应该使所有按钮的高度相同,以便使用col0 的总高度。不幸的是,使用您的代码,所有按钮的高度都变小了,以至于col0 的很大一部分仍然是黑色的。我该如何解决?每个按钮都有一个 ID 重要吗?我扩展了您的 for 循环并将按钮的引用保存在数组中。所以我仍然可以在没有 ID 的情况下访问它们。
  • id 不是强制性的。我根据你的要求得到了它。关于按钮大小问题我不知道。我猜你已经测试了布局,只有生成部分是问题
  • 是的,我确实测试过。如果我使用我的 XML 文件,它工作正常。但是用你的方法,一切似乎都是正确的,但高度。也许我们必须以某种方式告诉 col0,它应该在添加按钮后重新计算高度。
  • 我会尽快推进调查。
  • 确实没用。我没有找到基于styles 的解决方案,所以我将样式部分放在代码newButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1)); 中并删除了custombutton 模板。它现在应该可以工作了。解决方案已被编辑。
【解决方案2】:
int buttonsNumber = YOUR_BUTTONS_NUMBER;
LinearLayout col0 = (LinearLayout)findViewById(R.layout.col0);
Button button = new Button(getContext()); // or create xml view for button and get it with findViewById

// adding buttons
for(int i = 0; i < buttonsNumber; i++)
    col0.addView(button);

// setting weight
TextView v0 = (TextView)findViewById(R.layout.v0);
v0.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.match_parent, 0, buttonsNumber - 1));

【讨论】:

  • 首先,我们不应该一直添加相同的Button,但这没有问题可以解决。您的解决方案中的问题是新的 Buttons 看起来与 xml 布局生成的完全不同。
  • @principal-ideal-domain 那是因为样式尚未设置。尝试 findviewbyid 获取现有的 xml 按钮
  • 如何设置样式?你能发布一些代码吗?我不明白“尝试 findviewbyid 获取现有的 xml 按钮”是什么意思。我以为我们正在尝试在不使用 xml 使按钮数量动态化的情况下解决问题。
  • 您不能以编程方式设置视图的样式。我不知道你的“MyButton”风格是什么。尝试单独设置参数或为您的按钮创建 my_button.xml 并使用它 Button button = (Button)findViewById(R.layout.button);
  • 我尝试了您的findViewById 方法。但是这个方法在提供的外部布局文件中带有 Button 的 id 总是返回 null
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
相关资源
最近更新 更多