gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:25:35 24 4
gpt4 key购买 nike

我在一个 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/

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