繁体   English   中英

LinearLayout中的动态按钮数

[英]Dynamic number of buttons in LinearLayout

onCreate我的方法Activity ,我知道的数量Buttons ,我想在col0 在下面的示例中,它是四个Buttons 然后我的TextView v0android:layout_weight设置为number of buttons minus one (这是因为v1的高度应与所有Buttons高度相同)。 如果可以归纳出动态的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));
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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM