我有一个问题。当我从一个 fragment 滑动到另一个 fragment 时,背景会随着文本滑动,而不是静止不动。背景如何在所有 fragment 中都是静态的。我的错误是什么?
activity_main.xml
:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hintergrund" />
四个 fragment 之一(都有相同的代码):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="Test"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffdb00"
android:textStyle="italic" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="141dp"
android:layout_marginLeft="18dp"
android:text="Kollegah; King; Omega"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffdb00" />
</RelativeLayout>
新建 activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hintergrund" />
<ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hintergrund" />
</FrameLayout>
最佳答案
您可以使用 ImageView 将 viewpager 包装在 FrameLayout 中。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hintergrund" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</FrameLayout>
这样,ViewPager 将显示在静态的 ImageView 之上。
关于java - 为多个 fragment 使用静态背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701567/