java - 为多个 fragment 使用静态背景图像

标签 java android android-fragments background

我有一个问题。当我从一个 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/

相关文章:

java - Tomcat 8 似乎开始但不在 Eclipse Neon 上

android - 在小部件树 Flutter 中检测到重复的 GlobalKey

java - 如何在 android 环境中使用 jersey client api?

android - 如何将依赖项注入(inject)嵌套的android fragment ?

java - 从 InputStream 创建 Java 7 zip 文件系统

java - jme3 - UV 贴图错位在从 Blender 导出的模型上

java - 在 Jar 文件中获取 xml 文件

android - 带有 cordinator 布局的操作栏不会 "snap"在部分更改 android 时完全隐藏或完全显示

android - 清理动态添加的 fragment

android - 在 android : activity vs fragment 中使用底部导航的最佳实践