简体   繁体   English

从 LinearLayout 获取子元素

[英]Getting child elements from LinearLayout

Is there a way to obtain a child element of a LinearLayout?有没有办法获取 LinearLayout 的子元素? My code returns a view (linearlayout), but I need to get access to specific elements inside of the layout.我的代码返回一个视图(线性布局),但我需要访问布局内的特定元素。

Any suggestions?有什么建议么?

(Yes, I know I could use findViewById, but I am creating the layouts/children in java - not XML.) (是的,我知道我可以使用 findViewById,但我正在 java 中创建布局/子项 - 而不是 XML。)

You can always do something like this:你总是可以做这样的事情:

LinearLayout layout = setupLayout();
int count = layout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
    v = layout.getChildAt(i);
    //do something with your child element
}

I think this could help: findViewWithTag()我认为这可能会有所帮助: findViewWithTag()

Set TAG to every View you add to the layout and then get that View by the TAG as you would do using ID将 TAG 设置为添加到布局中的每个视图,然后像使用 ID 一样通过 TAG 获取该视图

LinearLayout layout = (LinearLayout)findViewById([whatever]);
for(int i=0;i<layout.getChildCount();i++)
    {
        Button b =  (Button)layout.getChildAt(i)
    }

If they are all buttons, otherwise cast to view and check for class如果它们都是按钮,否则投射到查看并检查 class

View v =  (View)layout.getChildAt(i);
if (v instanceof Button) {
     Button b = (Button) v;
}

I would avoid statically grabbing an element from the view's children.我会避免从视图的子项中静态地抓取一个元素。 It might work now, but makes the code difficult to maintain and susceptible to breaking on future releases.它现在可能可以工作,但会使代码难以维护并且容易在未来的版本中被破坏。 As stated above the proper way to do that is to set the tag and to get the view by the tag.如上所述,正确的方法是设置标签并通过标签获取视图。

You can do like this.你可以这样做。

ViewGroup layoutCont= (ViewGroup) findViewById(R.id.linearLayout);
getAllChildElements(layoutCont);
public static final void getAllChildElements(ViewGroup layoutCont) {
    if (layoutCont == null) return;

    final int mCount = layoutCont.getChildCount();

    // Loop through all of the children.
    for (int i = 0; i < mCount; ++i) {
        final View mChild = layoutCont.getChildAt(i);

        if (mChild instanceof ViewGroup) {
            // Recursively attempt another ViewGroup.
            setAppFont((ViewGroup) mChild, mFont);
        } else {
            // Set the font if it is a TextView.

        }
    }
}

One of the very direct ways is to access the child views with their ids.一种非常直接的方法是使用它们的 id 访问子视图。

view.findViewById(R.id.ID_OF_THE_CHILD_VIEW)

here view is the parent view.这里的视图是父视图。

So for instance your child view is an imageview, with id, img_profile, what you can do is:因此,例如,您的子视图是 imageview,带有 id、img_profile,您可以做的是:

ImageView imgProfile = view.findViewById(R.id.img_profile)

The solution in Kotlin version will be: Kotlin版本中的解决方案将是:

val layout: LinearLayout = setupLayout()
        val count = layout.childCount
        var v: View? = null
        for (i in 0 until count) {
            v = layout.getChildAt(i)
            //do something with your child element
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM