WebView全屏拉伸异常:已设Top_toBottomOf仍不符合预期(ConstraintLayout)
WebView 被拉伸至全屏的问题解决
问题原因
你设置的WebView属性android:layout_height="match_parent"会让它直接占据父布局的全部高度,完全忽略app:layout_constraintTop_toBottomOf的约束,所以才会覆盖上方的ChipGroup区域。
修复方案
把WebView的高度改为0dp(对应ConstraintLayout的match_constraint模式),同时添加底部约束对齐父布局底部,这样WebView就会自动填充从HorizontalScrollView底部到父布局底部的剩余空间,不会再拉伸覆盖顶部内容。
修改后的完整布局代码:
<?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="@color/background_activity_color" tools:context=".presentation.activities.MainActivity"> <HorizontalScrollView android:id="@+id/horizontal_scroll_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:scrollbars="none" android:theme="@style/Theme.MaterialComponents.Light" android:visibility="visible" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <com.google.android.material.chip.ChipGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chip_group" app:chipSpacingHorizontal="15dp" app:singleLine="true" app:singleSelection="true"> <com.google.android.material.chip.Chip style="@style/CustomStyleChip" android:id="@+id/first_chip" android:checkable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/google_name" /> </com.google.android.material.chip.ChipGroup> </HorizontalScrollView> <WebView android:layout_width="0dp" android:layout_height="0dp" android:id="@+id/webview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/horizontal_scroll_view" app:layout_constraintBottom_toBottomOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者Kurik9797
相关产品推荐
相关产品推荐

