ScrollView
ScrollView
ScrollView fillViewport 属性
为了屏幕适配,包含多元素的布局一般都会使用 ScrollView ,以便小屏手机滑动查看,但是在大屏手机上内容全部加载,导致下方空白
我们希望最后的 Button 是置底的,同时是可以跟随滑动的
不设置 viewport
当子布局高度小于 ScrollView 的高度时,定义子布局 match_parent 或者 fill_parent 不起作用,因此设置 layout_gravity 也不起作用
在 scrollview 里添加属性 android:fillViewport=”true”
就可以了,使得子布局高度和 scrollview 一样,而当子布局高度超过 scrollview 的高度时,这个属性就没有意义了
效果:
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/amber_900"
android:fillViewport="true"
tools:context=".samples.ui.scrollview.ScrollViewDemo">
<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_height="wrap_content">
<TextView
android:text="viewport true 哈哈 \n heh \n 动画 \n 解决\n dd\n 急急急\n"
android:background="@color/gray_500"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="哈哈 \n heh \n 动画 \n 解决\n dd\n 急急急\n"
android:background="@color/gray_500"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="哈哈 \n heh \n 动画 \n 解决\n dd\n 急急急\n"
android:background="@color/gray_500"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="哈哈 \n heh \n 动画 \n 解决\n dd\n 急急急\n"
android:background="@color/gray_500"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:visibility="visible"
android:layout_marginTop="20dp"
android:background="@color/indigo_A100"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="底部按钮"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="bottom"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
本文由作者按照 CC BY 4.0 进行授权