【问题标题】:Inflate a layout in a layout Android在 Android 布局中膨胀布局
【发布时间】:2015-07-01 13:41:32
【问题描述】:

我正在尝试在另一个布局中膨胀一个布局,但它不起作用... 在我的活动中,我显示了黑色的 activity_serie.xml,然后我尝试在白色的 image_item.xml 上充气。但我从未见过 image_item.xml。

public class Serie extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_serie);

    ViewPager mainLayout = (ViewPager) findViewById(R.id.serie_pager);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View menuLayout = inflater.inflate(R.layout.image_item, mainLayout, true);
}

@Override
protected void onResume() {
    super.onResume();
}}

activity_serie.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000
    android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/serie_pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>

image_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#FFFFFF"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</RelativeLayout>

谁能告诉我为什么? 谢谢。

【问题讨论】:

  • 你应该为你的图像视图设置一些东西吗?
  • 我已经删除了,我也有同样的行为

标签: android xml layout android-inflate


【解决方案1】:

findViewById(R.layout.activity_serie)

您应该在此处指定视图 ID,而不是布局名称。

【讨论】:

  • 当我在activity_serie.xml中添加一个RelativeLayout然后我膨胀它,它工作,但现在我将使用ViewPager,如果我这样做:ViewPager mainLayout = (ViewPager) findViewById(R .id.serie_pager); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);查看 menuLayout = inflater.inflate(R.id.image_Display, mainLayout, true);它再次不起作用
  • 您不能将 Layout 手动直接膨胀到 ViewPager 中,它们不是为此而设计的。您应该使用 PagerAdapter 来填充它。
【解决方案2】:

你为什么要这样做?只需将两个视图放入一个 xml,并为一个元素设置 android:visibility="gone"。您可以通过 id 来处理布局并根据需要更改可见性。

【讨论】:

  • 我改了问题,我这样做是因为我想使用 ViewPager 并在其上设置一个 PagerAdapter(用手指扫一些自定义图像),并且可以在活动期间更新图像生活。
猜你喜欢
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
相关资源
最近更新 更多