You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何让HorizontalScrollView子项填满屏幕且不重叠,每次仅显示一个?

解决HorizontalScrollView子项填满屏幕且不重叠的问题

嘿,我来帮你搞定这个难题!你现在遇到的问题核心在于LinearLayout的宽度设置不对,导致三个TextView挤在有限的空间里重叠了。咱们一步步来修改:

问题分析

原布局里,HorizontalScrollView的子LinearLayout设置了android:layout_width="match_parent",这就把LinearLayout的宽度限制在了屏幕范围内。再加上每个TextView都设为match_parent,三个TextView必然会挤在一起重叠,根本没办法横向滚动展示。

具体修改步骤

1. 调整LinearLayout的宽度

把LinearLayout的android:layout_widthmatch_parent改成wrap_content。这样LinearLayout的宽度会根据子View的总宽度自动扩展,HorizontalScrollView才能有可滚动的空间。

2. 确保每个TextView占满屏幕宽度

保留每个TextView的android:layout_width="match_parent",这样每个TextView都会单独占满一整个屏幕的宽度。同时可以去掉LinearLayout的android:gravity="center_horizontal",因为咱们不需要子View居中,而是要它们依次占满屏幕。

修改后的完整布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FDE0DFDF" 
    tools:context=".Main4Activity">

    <HorizontalScrollView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_marginTop="100dp" 
        app:layout_constraintBottom_toBottomOf="parent" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toTopOf="parent">

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

            <TextView 
                android:id="@+id/textView7" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:gravity="center_horizontal" 
                android:fontFamily="@font/alfa_slab_one" 
                android:text="TextView" 
                android:textColor="@android:color/black" 
                android:textSize="33sp" />

            <TextView 
                android:id="@+id/textView8" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:gravity="center_horizontal" 
                android:fontFamily="@font/alfa_slab_one" 
                android:text="TextView" 
                android:textColor="@android:color/black" 
                android:textSize="33sp" />

            <TextView 
                android:id="@+id/textView9" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:gravity="center_horizontal" 
                android:fontFamily="@font/alfa_slab_one" 
                android:text="TextView" 
                android:textColor="@android:color/black" 
                android:textSize="33sp" />
        </LinearLayout>
    </HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

额外补充(针对后续滚动需求)

如果你要实现按钮滚动到指定TextView,可以先获取屏幕宽度,然后用scrollTo()或者smoothScrollTo()方法:

// 获取屏幕宽度
val displayMetrics = resources.displayMetrics
val screenWidth = displayMetrics.widthPixels

// 滚动到第二个TextView(索引1),偏移量为screenWidth * 1
horizontalScrollView.scrollTo(screenWidth * 1, 0)
// 平滑滚动的话用smoothScrollTo
horizontalScrollView.smoothScrollTo(screenWidth * 1, 0)

这样修改后,每个TextView都会单独占满一整屏,横向滚动时不会重叠,完全符合你的需求!

内容的提问来源于stack exchange,提问作者Binyamin Robbins

火山引擎 最新活动