douqian8238 2012-05-11 18:02
浏览 28
已采纳

访问布局元素到N的深度

I have a pretty annoying problem here, as Im still trying to internalize each and every bits of what I am doing,

I currently have a LinearLayout, then upon onCreate of the Activity, I will populate or inflate several other LinearLayout's with Buttons, my problem is that when I try to access the button, it seems that I'm not getting any close or deeper from the LinearLayout, all I can get is the LinearLayout(Parent) and the other LinearLayout(Children), I believe there is a way, Im just totally confused how to do it.

LinearLayout
 ->LinearLayout(Child1)->Button1, Button2, Button3
 ->LinearLayout(Child2)->Button4, Button5, Button6

How would I be able to access and get the Buttons?

My source;

for (int x=0; x<ll.getChildCount(); x++){
  View v = ll.getChildAt(x);
  Class c = v.getClass();
  if(c == LinearLayout.class){
    for(int y=0; y< ; y++){
      **I know there is something that must be done here, likewise, is this the most
      efficient way of doing things?
    }
  }
 Log.i("test", c.getName());
}

Only the LinearLayout(Parent) exist in the XML, others are inflated run-time.

  • 写回答

1条回答 默认 最新

  • dongwuying0221 2012-05-11 18:20
    关注

    You should be able to simply cast v to a LinearLayout, then access its children just like you did with its parent. Something like:

    for (int x=0; x<ll.getChildCount(); x++){
      View v = ll.getChildAt(x);
      Class c = v.getClass();
      if(c == LinearLayout.class){
        //Cast to LinearLayout since View doesn't expose a way to access children
        LinearLayout innerLayout = (LinearLayout)v;
        for(int y=0; y<innerLayout.getChildCount() ; y++){
          Button b = (Button)innerLayout.getChildAt(y);
    
          //Do something with b
        }
      }
     Log.i("test", c.getName());
    }
    


    Depending on your exact hierarchy you could possibly simplify this by removing the reflection and simply doing a null check (if needed, wrap it in a try/catch and catch a ClassCastException). I've typically done something like this in situations where I need to traverse a layout tree that's dynamically generated:

    for (int i = 0; i < outerLayout.getChildCount(); ++i)
    {
        try
        {
            LinearLayout innerLayout = (LinearLayout)outerLayout.getChildAt(i);
    
            if (innerLayout != null)
            {
                for (int j = 0; j < innerLayout.getChildCount(); ++j)
                {
                    Button btn = (Button)innerLayout.getChildAt(j);
    
                    //Do something with btn
                }
            }
        }
        catch (ClassCastException cEx)
        {
            Log.w("WARN", "Unexpected child type in outerLayout. " + cEx.getMessage());
        }
    }
    

    This is an untested example (could need better exception handling depending on your requirements and layouts) but hopefully it gives you the general idea. If you want to be a little bit more type-agnostic you could also use a cast to ViewGroup instead. That would allow you to potentially use different kinds of layout containers as children, if needed, as they are subclasses of ViewGroup (which is where they inherit getChildAt() and getChildCount() from).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题