Android ScrollView隐藏了一些子布局
我正在尝试在Android ScrollView隐藏了一些子布局,android,android-layout,Android,Android Layout,我正在尝试在滚动视图中有几个布局。当然,ScrollView只能包含一个子布局,所以我就是这么做的: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <
滚动视图
中有几个布局。当然,ScrollView
只能包含一个子布局,所以我就是这么做的:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:minHeight="50dp"
android:text="Top layout" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bottom layout"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</ScrollView>
基本上我有一个LinearLayout
,它包含另外两个LinearLayout
s(“顶部”和“底部”)
我希望顶部布局占据尽可能多的空间,将底部布局保留在最底部。
问题:通过到处设置android:layout\u height=“match\u parent”,顶部布局占据所有空间,底部布局不显示
我怎样才能解决这个问题
编辑:保持布局高度固定不是我想要的。尺寸必须能够垂直延伸。尝试以下方法:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_gravity="top|center"
android:layout_height="472dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_gravity="bottom|center"
android:layout_height="100dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView4" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
您将顶部布局的高度设置为
匹配父级。这就是为什么它将底部布局从屏幕上推出。使用layout\u height=“0dp”
和layout\u weight=“1”
代替
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:minHeight="50dp"
android:text="Top layout" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bottom layout"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</ScrollView>
在第一个chld布局中,需要添加android:layout_weight=“1”使用relativelayout作为scrollview的子级,一切都会正常。您基本上设置了一个固定的高度,对吗?我也不想要这个。