myc_100 2013-01-10 05:06 采纳率: 10%
浏览 4186
已采纳

使用java代码如果获取所有的Button,例如childAt()方法?

我根据Button当前的高度,想为所有的Button重新设置背景高度,用下面的方法获取所有的Button对象:

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

我是想获取所有嵌套布局中的所有Button,但是在一些布局中既有Button又有 textviews。如何能只获取Button,而没有其它的?
谢谢!

  • 写回答

4条回答 默认 最新

  • Curie-87 2013-01-14 02:31
    关注

    添加一个新类和 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);
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?