android - 如何使 ScrollView 中的项目位于屏幕中央

标签 android android-layout android-scrollview

我在一个 Horizo​​ntalScrollView 中有三个 CardView,我希望中间的一个位于屏幕中央(就好像用户已经滑动才能到达它一样) 。如何在 XML 中或以编程方式执行此操作?

XML 布局:

<HorizontalScrollView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:scrollbars="none"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/culture_toolbar">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <androidx.cardview.widget.CardView
            android:id="@+id/one"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_marginEnd="10dp"
            app:cardCornerRadius="12dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/beach_bg_placeholder" />

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:id="@+id/two"
            android:layout_width="300dp"
            android:layout_height="300dp"
            app:cardCornerRadius="12dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/beach_bg_placeholder" />

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:id="@+id/three"
            android:layout_width="300dp"
            android:layout_height="300dp"
            app:cardCornerRadius="12dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/beach_bg_placeholder" />

        </androidx.cardview.widget.CardView>
    </LinearLayout>
</HorizontalScrollView>

这就是我想要的结果:

I want the upper half the mockup

最佳答案

How do i do that in xml preferably?

据我所知,仅使用 XML 是不可能的

How to make an item in a ScrollView be positioned in the center of the screen

我建议您应该使用轮播布局而不是使用Horizo​​ntalScrollView

您可以制作carousel layout using ViewPager and using RecyclerView

查看以下有关轮播布局的好文章

关于android - 如何使 ScrollView 中的项目位于屏幕中央,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57345448/

相关文章:

android - 如何在 Android 上将 socket.io 作为连续服务运行而无需 sleep 或暂停?

java - Android - 2 小时后获取时间

Android水平 ScrollView 像画廊

android - Android ScrollView 中的 VerticalScrollExtent 是什么?

android - 将按钮 onClick 传递回主要 Activity 的最佳方式?

android - android 约束布局 1.1.3 的组可见性不起作用

android - 以编程方式创建 View 时,如何知道要使用哪个 LayoutParams 类?

android - 在 Android 中更改编辑文本样式

java - 我的调色板中的小部件标题(例如(TexField、布局、表单小部件以及图像和媒体))为空,我该如何解决此问题?178173

android - 隐藏 ScrollView 棒在某些设备上会出现空指针异常