我根據Button當前的高度,想為所有的Button重新設置背景高度,用下面的方法獲取所有的Button對象:
for(int i=0;i<layout.getChildCount();i++)
{
Button b = layout.childAt(i);
b.setHeight(x);
}
我是想獲取所有嵌套布局中的所有Button,但是在一些布局中既有Button又有 textviews。如何能只獲取Button,而沒有其它的?
謝謝!
添加一個新類和 extend 按鈕,然後重新你需要的方法。
在我的例子中我重新了 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);
}