android - xml - 多次包含相同的布局不起作用

标签 android xml layout include

我尝试两次包含以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<com.viewpagerindicator.TabPageIndicator
    android:id="@+id/indicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

喜欢下面的

<include
    android:id="@+id/include1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/view_pager" />

<include
    android:id="@+id/include2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/view_pager" />

实际上,如果我那样做,第二个 View 寻呼机将无法工作...指标 2) 一切正常。有更好的方法吗?复制布局来实现这一点似乎使包含对同一布局的多个包含无用....

我认为我得到的引用是正确的,但是如果我包含相同的布局它就不起作用......

pager1= (ViewPager)(findViewById(R.id.include1).findViewById(R.id.pager));
pager2= (ViewPager)(findViewById(R.id.include2).findViewById(R.id.pager));

如果我复制布局,一切都会完美...

编辑:

我认为这与 FragmentManager 有关,因为 View 寻呼机具有相同的 id...但我不知道如何正确解决...

最佳答案

是的,这是可以做到的。您可以多次膨胀布局,但必须以编程方式进行包含。查看answer同样的问题。

关于android - xml - 多次包含相同的布局不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17171817/

相关文章:

java - 如何从使用 SimpleAdapter 的 Listview 获取文本 onClick

android - 当 wifi SSID 改变时执行代码

java - 如何在 SQLite android 中管理列表?

java - Android 可折叠工具栏菜单图标消失

css - 如何让 Flexbox 元素 "main"让 "aside" float 在它旁边?

javascript - 使用 JavaScript 在 Android 4.0+ WebView 中检测键盘事件

java - Tomcat 和 vHost 在特定子目录上失败

Android findViewWithTag 返回 null

android - 两个复选框,只能选择一个

java - JLabel 到 GridBagLayout。如何固定宽度?