【问题标题】:How to add buttons with for loops to a layer in Android如何将带有for循环的按钮添加到Android中的图层
【发布时间】:2015-04-24 21:55:34
【问题描述】:

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

如何在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。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 其中一个挑战是创建一个圆形按钮,这非常复杂,因为当您对普通圆形按钮施加重量时,圆度会被破坏。或者只是尝试使其大小动态设置。感谢您的链接,我从这里找到了一些对我有帮助的东西。
    • 您可以为按钮创建“圆形按钮可绘制”并将其应用于您正在添加的按钮。
    • 是的,我认为这就是他们在 markushi.ui.CircleButton 中所做的
    • 您可以像 yourBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_button)) 一样以编程方式应用可绘制圆形按钮;
    【解决方案2】:

    找到了一种不损害设计的方法。 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"
                    />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多