简体   繁体   English

如何以线性布局访问膨胀视图

[英]How to access inflated views in a linear layout

I use layout inflater, to inflate a linear layout with some buttons and some textviews in it. 我使用布局填充器,在其中包含一些按钮和一些textview的线性布局中充气。 How can i access the widgets in the inflated layout? 我如何访问膨胀布局中的小部件?

For example : I have a button and i want to set onclick listener for it. 例如 :我有一个按钮,我想为其设置onclick侦听器。 Normally you declare the button with 'findViewById' and then you set the listener. 通常,您使用“ findViewById”声明按钮,然后设置侦听器。 But the inflated buttons aren't present in the activity xml (the linear layout is in a different xml rather than the activity xml) to find their view with 'findViewById'. 但是活动xml中不存在膨胀的按钮(线性布局位于不同的xml中,而不是活动xml中)以查找带有'findViewById'的视图。

My inflater code: 我的充气机代码:

 LinearLayout item = (LinearLayout )findViewById(R.id.linear_layout_duel_basic_easy);
            View child_1 = getLayoutInflater().inflate(R.layout.linear_layout_duel_1, null);
            item.addView(child_1);

The layout i want to inlfate : 我想夸大的布局:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />
</LinearLayout>

Thanks for any help. 谢谢你的帮助。

It should work. 它应该工作。 There is nothing special in here. 这里没有什么特别的。

   LinearLayout item = findViewById(R.id.linear_layout_duel_basic_easy);
    for (int i = 0 ; i < 10; i++) {
        View child = getLayoutInflater().inflate(R.layout.linear_layout_duel_1, null);
        item.addView(child);
        final int finalI = i;
        child.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "clicked = " + finalI, Toast.LENGTH_LONG).show();
            }
        });
    }

According to the comment: 根据评论:

In the activity xml i have a vertical linear layout (created for scroll view) and in the xml that i use to inflate a have a horizontal linear layout where a button and a textview are 在活动xml中,我具有垂直线性布局(为滚动视图创建),而在用于膨胀的xml中,我具有水平线性布局,其中按钮和textview是

Here is how you can achieve it. 这是您如何实现的。

LinearLayout item = (LinearLayout)findViewById(R.id.linear_layout_duel_basic_easy); // Your vertical linearLayout
View child_1 = getLayoutInflater().inflate(R.layout.linear_layout_duel_1, null);// Your Horizontal LinearLayout
item.addView(child_1);

To access the view inside horizontal LinearLayout:- 要访问水平LinearLayout内部的视图,请执行以下操作:

TextView tv = child_1.findViewById(R.id.textView);
Button btn = child_1.findViewById(R.id.button);

Have you tried overriding onFinishInflate() ? 您是否尝试覆盖onFinishInflate()

Inside you can look up for the inflated button, you can simply iterate all your children to find the view you need. 在内部,您可以查找膨胀的按钮,您可以简单地遍历所有孩子以找到所需的视图。

//iterate children
for (int index = 0; index < getChildCount(); index+=1){
            getChildAt(index);
//check if that view is what you need
        }

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

相关问题 如何从以编程方式夸大的布局访问视图? - How to access views from the programatically inflated layouts? 如何访问膨胀布局内的TextView的值 - How to access the value of a TextView that is inside an inflated layout 如何关闭充气布局? - How to close inflated layout? 线性布局在空白视图中的行为如何? - How does Linear Layout behaves with empty views? 如何获取当前在回收站视图的线性布局中不可见的视图 - How to get the views which are not visible currently in the linear layout of the recycler view 如何获取多个膨胀视图的ID? - How to get ids of multiple inflated views? 如何删除已经放大到屏幕的布局 - How to remove a layout that have been inflated to the screen 如何从布局中获取膨胀的片段引用? - How to get the inflated Fragment Reference from Layout? 我如何在线性布局(根视图)中包含两个线性布局(嵌套视图)下方的按钮 position - how can i position a button below two linear layouts(nested views) containted in a linear layout (root view) 如何创建自定义首选项布局并以编程方式访问该布局中的视图? - How to create a custom Preference layout and access the views within that layout programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM