【问题标题】:How to access inflated views in a linear layout如何在线性布局中访问膨胀视图
【发布时间】:2018-04-30 08:12:08
【问题描述】:

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

例如:我有一个按钮,我想为它设置 onclick 监听器。通常,您使用“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>

感谢您的帮助。

【问题讨论】:

  • 试试 child_1.findViewById(R.id.btn_id)

标签: java android android-inflate


【解决方案1】:

它应该工作。这里没有什么特别的。

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

【讨论】:

  • 如果我再次膨胀它会产生错误:指定的孩子已经有一个父母。
  • @ManosRasidakis 编辑了答案。查看。如果可行,请接受答案。
  • 如果我想将布局膨胀 10 次,我必须让 child_1,child_2,...child_10?
  • @ManosRasidakis 更新了多次。 “finalI”是该位置的参考。点击哪个按钮的顺序。
【解决方案2】:

根据评论:

在活动 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);

【讨论】:

  • 我认为如果我这样做,我会给 linear_layout 一个 id 而不是它里面的按钮
  • child_1 是按钮吗?
  • 没有。它是一个包含按钮的线性布局
  • 好的,那么按钮在哪里呢?在您的 sn-p 中,item 是 LinearLayout,您正在将视图 child_1 添加到 LinearLayout。
  • 在活动 xml 中,我有一个 vertical linear layout(为滚动视图创建),在我用来充气的 xml 中有一个水平 线性布局 按钮和文本视图所在的位置。我想在垂直和水平之间膨胀并访问新按钮。
【解决方案3】:

您是否尝试过覆盖onFinishInflate()

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

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

【讨论】:

  • 如何迭代?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
相关资源
最近更新 更多