Android 忽略父视图高度
我有自定义的Android 忽略父视图高度,android,android-layout,android-scrollview,Android,Android Layout,Android Scrollview,我有自定义的LinearLayout,它是我的子视图的父视图。我正在使用parent.addView()通过膨胀子布局将子视图添加到此父视图视图中。这个LinearLayout被包装在ScrollView中,因此如果有更多的子视图,我可以滚动它们。问题是,如果我在那里添加子视图(由于某种原因,它被拉伸到全屏高度),则忽略了LinearLayoutheight。这种行为完全破坏了对ScrollView的需求。为什么会这样?我需要一些maxHeight参数才能使其工作。它只能拉伸到最大200dp 布
LinearLayout
,它是我的子视图的父视图。我正在使用parent.addView()
通过膨胀子布局将子视图添加到此父视图视图中。这个LinearLayout
被包装在ScrollView
中,因此如果有更多的子视图,我可以滚动它们。问题是,如果我在那里添加子视图(由于某种原因,它被拉伸到全屏高度),则忽略了LinearLayout
height。这种行为完全破坏了对ScrollView
的需求。为什么会这样?我需要一些maxHeight
参数才能使其工作。它只能拉伸到最大200dp
布局:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/resultList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:paddingTop="@dimen/padding_medium"
android:paddingStart="@dimen/padding_medium"
android:paddingEnd="@dimen/padding_medium"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
您可以通过使用属性app:layout\u constraintHeight\u max=“200dp”使用ConstraintLayout的根和ScrollView的子级来实现此行为,ScrollView子级将是带有layout\u heights\u wrap\u内容的线性布局
以下是xml布局:
<?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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
app:layout_constraintHeight_max="200dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:id="@+id/resultList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/padding_medium"
android:paddingStart="@dimen/padding_medium"
android:paddingEnd="@dimen/padding_medium"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
将9个高度为50dp、颜色不同的子视图添加到总高度大于200dp的线性布局后,结果如下(需要滚动)(滚动视图的最大高度为200dp):
从您的问题中不清楚布局的目标是什么。在我看来,ScrollView对其子视图的高度进行了一些假设(并且它只能有一个子视图)。如果您希望LinearLayout具有特定的高度,请在ScrollView本身上设置该高度。在您的recyclerView&Chnage ScrollView高度中使用android:fillViewport=“true”以匹配父视图/0dp。希望这对你有用