【问题标题】:How to get a view from content_main.xml如何从 content_main.xml 获取视图
【发布时间】:2016-02-28 04:16:49
【问题描述】:

我们在 Android Studio 1.4 新模式中添加了 conten_main.xml 文件。

我需要从该文件中获取FrameLayout 视图,但通过findViewById() 这是不可能的。因为ActivityMain.javaactivity_main.xml 布局,其中包括 app_bar_activity_main.xml 布局,其中包括content_main.xml 布局。

如何从content_main.xml 获得FrameLayout 视图?

【问题讨论】:

  • 如果框架布局直接存在于activity_main.xml 中,您可以按常规方式获取框架布局。将 if 分配给框架布局并使用 findView by id 访问它。

标签: android xml android-layout view widget


【解决方案1】:

要访问您包含的布局,请添加一个 id,如下所示:

<include 
    android:id="+id/id_content_main"
    layout="@layout/content_main" />

然后像这样访问它

View contentView = findViewById(R.id.id_content_main);
FrameLayout layout = (FrameLayout)contentView.findViewById(R.id.frame_layout_id);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多