gpt4 book ai didi

android - 如何在ViewPager中设置固定背景

转载 作者:行者123 更新时间:2023-11-29 23:30:32 25 4
gpt4 key购买 nike

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

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

最佳答案

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

只需创建一个包含两个子项的 FrameLayout:您的 ViewPager 和您要用作 viewpager 背景的 View 。这是一个例子:

<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 替换为包含您希望背景包含的所有复合 View 的布局(例如 RelativeLayout)。

希望这对您有所帮助.. 编码愉快!

关于android - 如何在ViewPager中设置固定背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52770037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com