Android ScrollView无法滚动问题求助(已尝试Stack Overflow所有方案)
ScrollView无法滚动的解决方案
你的ScrollView无法滚动的核心问题在于内部LinearLayout的权重配置错误,导致布局高度被强制限制在ScrollView可视范围内,无法触发滚动。以下是具体修改方案:
问题分析
- 内部LinearLayout设置了
android:weightSum="25",但所有子View的layout_weight总和远大于25(第一个TextView占15,后续还有7、10、8等,总和远超设定值) - LinearLayout的
android:layout_height="wrap_content"搭配layout_weight会让系统强制按权重分配空间,把所有内容压缩到ScrollView的高度内,导致内容不会超出可视区域,自然无法滚动
修改步骤
- 移除不必要的纵向权重配置:删除内部LinearLayout的
android:weightSum="25",同时删除所有纵向子View的android:layout_weight属性 - 调整横向布局的ImageView:将横向LinearLayout中ImageView的
layout_width设为0dp,保留layout_weight以实现平分横向空间的效果,同时将layout_height设为wrap_content避免占用过多空间 - 保留ScrollView核心配置:保持
android:fillViewport="true"和约束布局的配置,确保ScrollView占满父容器
修改后的完整代码
<?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" tools:context=".arabicnamazfour"> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:fillViewport="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:animateLayoutChanges="true" android:orientation="vertical"> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:gravity="center_horizontal" android:text="رکوع کی دعا" android:textStyle="bold|italic" /> <View android:id="@+id/divider2" android:layout_width="match_parent" android:layout_height="6dp" android:background="?android:attr/listDivider" android:backgroundTint="#32728F" android:backgroundTintMode="src_over" /> <TextView android:id="@+id/textView19" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:text="سُبْحَانَ رَبِّيَ الْعَظِیْمِ ۔ (مسلم) کم از کم تین مرتبہ" /> <TextView android:id="@+id/textView21" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:gravity="center_horizontal" android:text="یا دعا پڑھے۔ " android:textStyle="bold" /> <TextView android:id="@+id/textView23" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:text="سُبْحَانَکَ اَللّٰہُمَّ رَبَّنَا وَبِحَمْدِکَ اَللّٰہُمَّ اغْفِرْلِیْ ۔ (بخاری و مسلم)" /> <TextView android:id="@+id/textView25" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:gravity="center_horizontal" android:text="رکوع کے بعد کی دعا" android:textStyle="bold" /> <TextView android:id="@+id/textView27" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:text="رَبَّنَاوَ لَکَ الْحَمْدُ ۔" /> <TextView android:id="@+id/textView28" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:gravity="center_horizontal" android:text="یا یہ کہے۔" android:textStyle="bold" /> <TextView android:id="@+id/textView29" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:text="رَبَّنَا وَ لَکَ الْحَمْدُ‘ حَمْدًاکَثِیْرًا طَیِّبًا مُّبَارَکًا فِیْہ ۔ (بخاری)" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2"> <ImageView android:id="@+id/imageView7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" app:srcCompat="@drawable/fourth" /> <ImageView android:id="@+id/imageView8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" app:srcCompat="@drawable/fifth" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2"> <ImageView android:id="@+id/imageView9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" app:srcCompat="@drawable/sixth" /> <ImageView android:id="@+id/imageView10" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" app:srcCompat="@drawable/seventh" /> </LinearLayout> <TextView android:id="@+id/textView13" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:backgroundTint="#32728F" android:gravity="center_horizontal" android:text="4" android:textStyle="bold" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2"> <ImageView android:id="@+id/imageView17" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" app:srcCompat="@drawable/ic_baseline_navigate_before_24" /> <ImageView android:id="@+id/imageView18" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" app:srcCompat="@drawable/ic_baseline_navigate_next_24" /> </LinearLayout> </LinearLayout> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout>
额外说明
- 移除纵向LinearLayout的权重后,布局会根据内容自然延伸,当内容高度超过ScrollView的可视高度时,就会自动触发滚动
- 横向LinearLayout中的ImageView保留权重配置,是为了让它们平分横向空间,适配不同尺寸的屏幕
内容的提问来源于stack exchange,提问作者Mohommed Habib
相关产品推荐
相关产品推荐

