代码之家  ›  专栏  ›  技术社区  ›  Taslim Oseni

如何在ViewPager中设置固定背景

  •  1
  • Taslim Oseni  · 技术社区  · 6 年前

    我有一个片段,它包含一个ViewPager,允许用户浏览三个片段。

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/blue"
        tools:context=".fragments.cash.WalletFragment">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:background="@drawable/revenue"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </RelativeLayout>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Taslim Oseni    6 年前

    虽然有很多方法可以实现这一壮举,但这种方法对我很有效:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/blue"
        tools:context=".fragments.cash.WalletFragment">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/my_smiling_face"/>
    
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </FrameLayout>
    
    </RelativeLayout>
    

    请注意,可以将Imageview替换为包含希望背景包含的所有复合视图的布局(例如RelativeLayout)。

    我希望这有帮助。。快乐的编码!