gpt4 book ai didi

android - 通过代码 [Android] 在另一个 XML 布局中获取 XML 布局

转载 作者:行者123 更新时间:2023-11-29 00:31:49 26 4
gpt4 key购买 nike

我有一个 xml 文件 main_layout.xml,在这个文件中我有另一个 xml 布局 popup_layout.xml,它是由以下人员添加的:

 <include
android:id="@+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
layout="@layout/popup_layout" />

在 MainActivity.java 文件中,我想获取弹出对象,以便我可以更改它的文本字段等。我该写什么?

我测试过

        View v = findViewById(R.id.popup);
TextView tv= (TextView) v.findViewById(R.id.ip_answer);

但是没用

最佳答案

include 的 id 属性不是必需的。将 id 属性添加到您要包含的布局中,然后通过该 id 查找 View 并根据布局类型相应地收集它
例如:

RelativeLayout/LinearLayout= findViewById(id);

然后找到 TextView 。
这应该有效

关于android - 通过代码 [Android] 在另一个 XML 布局中获取 XML 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15133294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com