【问题标题】:Get XML layout inside another XML layout by code [Android]通过代码在另一个 XML 布局中获取 XML 布局 [Android]
【发布时间】:2013-02-28 10:38:06
【问题描述】:

我有一个 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);

但它不起作用

【问题讨论】:

  • popup_layout.xml 文件的内容是什么?
  • 也许您可以使用单独的 XML 文件和 layoutInflater,而不是使用包含?
  • 这是一个RelativeLayout,有一些TextViews和一个按钮,没什么复杂的

标签: android xml view


【解决方案1】:

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

RelativeLayout/LinearLayout= findViewById(id);

然后找到文本视图。
这应该工作

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多