Java 如何访问线性布局中的膨胀视图

Java 如何访问线性布局中的膨胀视图,java,android,android-inflate,Java,Android,Android Inflate,我使用布局充气器,用一些按钮和一些文本视图充气线性布局。如何访问膨胀布局中的小部件 例如:我有一个按钮,我想为它设置onclick listener。通常使用“findViewById”声明按钮,然后设置侦听器。但是充气按钮不在活动xml中。线性布局位于不同的xml中,而不是活动xml中,以便使用“findViewById”查找它们的视图 我的充气机代码: LinearLayout item = (LinearLayout )findViewById(R.id.linear_layout_du

我使用布局充气器,用一些按钮和一些文本视图充气线性布局。如何访问膨胀布局中的小部件

例如:我有一个按钮,我想为它设置onclick listener。通常使用“findViewById”声明按钮,然后设置侦听器。但是充气按钮不在活动xml中。线性布局位于不同的xml中,而不是活动xml中,以便使用“findViewById”查找它们的视图

我的充气机代码:

 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);
我想要显示的布局:

<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>
谢谢您的帮助。

您是否尝试过覆盖充气

在里面你可以查找充气按钮,你可以简单地迭代你所有的孩子来找到你需要的视图

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

根据评论:

在活动xml中,我创建了一个垂直线性布局 滚动视图,在我用来充气的xml中有一个水平 线性布局,其中包含按钮和文本视图

以下是您如何实现它的方法

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);
要访问水平线布局内部的视图,请执行以下操作:-

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

它应该会起作用。这里没有什么特别的

   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();
            }
        });
    }

尝试child_1.findviewbydr.id.btn_id如何迭代?我认为如果我这样做,我将为线性布局提供一个id,而不是其中的按钮child_1是一个按钮?不。它是一个包含按钮的线性布局。那么按钮在哪里?在您的代码段中,项目是LinearLayout,您正在将视图子元素添加到LinearLayout中。在活动xml中,我为滚动视图创建了一个垂直线性布局,在我用于充气的xml中,我有一个水平线性布局,其中有一个按钮和一个文本视图。我想在垂直方向上放大水平方向并访问新按钮。如果再次放大,则会产生错误:指定的子项已经有父项。@Manosrasidak编辑了答案。检查如果有效,请接受答案。如果我想将布局充气10次,我必须使child_1、child_2、…child_10?@ManosRasidakis更新多次。finalI是该职位的参考。单击按钮的顺序。