2014-11-02 85 views
2

我遇到了問題。當我從一個片段滑動到另一個片段時,背景與文本一起滑動,而不是靜止。背景如何在所有的碎片中都是靜態的。我的錯誤是什麼?爲幾個片段使用靜態背景圖片

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" /> 

一個(都具有相同的代碼):

<?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> 

回答

1

你可以用你的viewpager在一個帶有ImageView的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上,它將是靜態的。

+0

我更新了我的舊帖子。不幸的是,第2行有一個錯誤: 錯誤:解析XML時出錯:未綁定的前綴。 感謝您的幫助。 – 2014-11-02 17:47:41

+0

@DominikKu將xmlns:android =「http://schemas.android.com/apk/res/android」移動到FrameLayout。 – Robbe 2014-11-02 17:51:24

+0

@DominikKu並從您的ViewPager中刪除背景屬性 – Robbe 2014-11-02 17:53:51