滚动吧!
目标人群:没有基础的安卓初学者
知识点:ScrollView的使用
目标:当控件超出页面显示范围时,页面可以滑动
简介
ScrollView的使用
HorizontalScrollView的使用
正文
1.首先要说明的是ScrollView是一个layout,添加一个ScrollView将activity_main.xml内部的元素包裹起来,代码如下。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="随便说点什么吧?"
android:id="@+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="800dp"
android:text="下一页"
android:onClick="goHello"
android:id="@+id/button" />
</LinearLayout>
</ScrollView>
ScrollView 的内部只可以有一个子元素,在本页面中为LinearLayout
由于是纵向滑动,LinearLayout的android:layout_height值应为wrap_content
当控件的高度超过屏幕高度时,ScrollView 的效果才可被观察到,因此Button控件的高度被设置成了一个较大的值
2.如果当控件的宽度超过屏幕宽度时,我们需要左右滑动页面,此时需要用到HorizontalScrollView,代码如下
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="随便说点什么吧?"
android:id="@+id/editText" />
<Button
android:layout_width="800dp"
android:layout_height="wrap_content"
android:text="下一页"
android:onClick="goHello"
android:id="@+id/button" />
</LinearLayout>
</HorizontalScrollView>
同上一页面相比,LinearLayout的android:layout_height应为fill_parent,而android:layout_width应为wrap_content
Button控件的宽度被改为了800dp
3.如果觉得滚动条的样子不是很美观,可以将ScrollView中添加android:scrollbars=”none”属性,实现
隐藏滚动条的作用