【问题标题】:How to set a fixed background in ViewPager如何在 ViewPager 中设置固定背景
【发布时间】:2018-10-12 07:05:28
【问题描述】:

我有一个包含 ViewPager 的 Fragment,它允许用户滑动浏览三个 Fragment。

问题是:我希望 ViewPager 的背景在用户滑动浏览片段时保持固定。我尝试在我的可绘制对象中设置一个图像作为 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>

【问题讨论】:

    标签: android xml background android-viewpager


    【解决方案1】:

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

    只需创建一个带有两个子项的 FrameLayout:您的 ViewPager 和您想要用作 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">
    
        <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)。

    我希望这会有所帮助.. 编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多