0

我想在 Button inflated 的高度上为所有按钮设置 drawable 的高度。我可以得到所有像这样的按钮对象

for(int i=0;i<layout.getChildCount();i++)
{
Button b = layout.childAt(i);
b.setHeight(x);
}

我的问题是在所有嵌套布局中获取所有唯一按钮。在某些布局中也有 Buttons 和 textvies,我怎样才能只访问按钮?

有人可以帮帮我吗?

4

3 回答 3

3

我不明白你为什么要这样做,但你可以试试这个,

for(int i=0; i < layout.getChildCount(); i++)
{
    View v = layout.getChildAt(i);
    if (v instanceof Button) {
        Button b = (Button) v;
        b.setHeight(x);
    }
}
于 2012-12-12T07:01:31.677 回答
0

初始化按钮

......
Button btn1 = (Button) findViewById(R.id.myBtn1);
Button btn2 = (Button) findViewById(R.id.myBtn2);
List<Button> btns = new ArrayList<Button>();
btns.add(btn1);
btns.add(btn2);
......

操作第二个按钮

int i=2;
btns.get(i).setWidth(100);
btns.get(i).setHeight(100);
于 2013-01-14T02:33:30.560 回答
-1

添加了一个新类并扩展了 Button ,然后覆盖了您想要的方法:)

在我的情况下,我覆盖了 setBackgroundDrawable()

@Override
public void setBackgroundDrawable(Drawable background) {


Log.d("button","setting new background  ");
Drawable[] layers = new Drawable[5];
Resources resources = getResources();




layers[0] = resources.getDrawable(R.drawable.outer_rectangle);
layers[1] = resources.getDrawable(R.drawable.inner_rectangle);
layers[2] = resources.getDrawable(R.drawable.upper_ovel);
layers[3] = resources.getDrawable(R.drawable.gradient_fill);
layers[4] = resources.getDrawable(R.drawable.lower_ovel);

LayerDrawable layerDrawable = new LayerDrawable(layers);

layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 3, 3, 3, 0);
layerDrawable.setLayerInset(2, 3, 15, 3, 25);
layerDrawable.setLayerInset(3, 3, 23, 3, 0);
layerDrawable.setLayerInset(4, 4, 60, 4, -5);



super.setBackgroundDrawable(layerDrawable);
}  
于 2012-12-12T11:44:50.417 回答