如何将带有for循环的按钮添加到Android中的图层

时间:2015-04-24 21:55:35

标签: android android-layout android-button android-layout-weight

我想根据需要创建许多宽度和高度恒定的按钮。

如何在android中执行以下操作?

for(row =0; row*buttonSizeY<otherButtonYCoardinate;row++){
    addButtonsToRow();
}

do{
    row++;
    addButtonsToRow();
}while(stillNeedToAddButton);

void addButtonsToRow(){
    for(col=0;col*buttonSizeX<screenWidth;col++){
        //instantiate button and set its X,Y coords according to row and col 
        relativeLayer.add(button);
    }
}

我没有找到按需按钮添加布局的真实方法。我希望上面有TableLayout并添加LinearLayouts,只要我有空间,但我没有找到一种方法来包括而不指定XML中的所有LinearLayout-s。我的屏幕宽度间距也有问题:我的按钮具有恒定的高度和宽度,因为它的椭圆形。所以我不能使用layout_weight。

2 个答案:

答案 0 :(得分:1)

您可以根据需要创建和添加按钮,并且需要规格。请检查https://stackoverflow.com/a/7198409/1878148

答案 1 :(得分:0)

找到一种不伤害设计的方法。 markushi.ui.CircleButton允许设置重量而不会损害按钮的圆度,当我尝试以相同的方式实现它时,它变成了椭圆形。

唯一的缺点是上面的插件需要api lvl 11而我使用的是lvl 9.

   <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/login_button"
        android:orientation="vertical"
        android:id="@+id/login_table_layout"
        >

        <TableRow

            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:layout_weight="33" 

            >

            <at.markushi.ui.CircleButton
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="20"  
                app:cb_color="#99CC00"
                app:cb_pressedRingWidth="8dip"
                />