Java 从 LinearLayout 获取子元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6615723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:17:51  来源:igfitidea点击:

Getting child elements from LinearLayout

javaandroidandroid-linearlayout

提问by Cody

Is there a way to obtain a child element of a LinearLayout? My code returns a view (linearlayout), but I need to get access to specific elements inside of the layout.

有没有办法获得 LinearLayout 的子元素?我的代码返回一个视图(线性布局),但我需要访问布局内的特定元素。

Any suggestions?

有什么建议?

(Yes, I know I could use findViewById, but I am creating the layouts/children in java - not XML.)

(是的,我知道我可以使用 findViewById,但我是在 Java 中创建布局/子项 - 而不是 XML。)

采纳答案by Aleks G

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
}

回答by Asahi

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 获取该视图

回答by THE_DOM

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.

我会避免从视图的子元素中静态抓取元素。它现在可能工作,但使代码难以维护并且容易在未来的版本中被破坏。如上所述,正确的方法是设置标签并通过标签获取视图。

回答by Ali

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.

        }
    }
}

回答by Anna Billstrom

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

如果它们都是按钮,否则强制转换以查看和检查类

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

回答by Anubhav

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:

因此,例如您的子视图是一个图像视图,带有 id、img_profile,您可以做的是:

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