目录
1 章节目录
-
ScollView控件
-
ProgressBar进度条
-
PopupWindow控件
2 ScollView控件
2.1 ScrollView简介
-
ScrollView就是可以滚动的用户布局容器
-
如果手机显示不下子布局,那么可以使用scrollView
2.2 ScrollView使用
-
不要在scrollView中添加
RecyclerView
或者是ListView
布局,这样会引起不好的体验因为会有滑动冲突的问题出现。 -
ScrollView的直接子View只能有一个。也就是说如果你要在滚动视图中添加多个视图,你需要一个标准的容器,如
LinearLayout
、RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World1" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World2" />
</LinearLayout>
</ScrollView>
</LinearLayout>
-
ScrollView只支持竖直滑动,想要水平滑动需要使用
HorizontalScrollView
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
2.3 常用属性及方法
可以直接利用ScrollView给我们提供的:fullScroll()方法:
scrollView.fullScroll(ScrollView.FOCUS_DOWN);滚动到底部
scrollView.fullScroll(ScrollView.FOCUS_UP);滚动到顶部
隐藏滑块:
android:scrollbars="none"
设置滚动速度: 继承ScrollView,然后重写一个 public void fling (int velocityY)的方法:
@Override
public void fling(int velocityY) {
super.fling(velocityY / 2); //速度变为原来的一半
}
public class ScrollViewActivity extends AppCompatActivity implements
View.OnClickListener {
private Button btn_down;
private Button btn_up;
private ScrollView scrollView;
private TextView txt_show;
@Override
public void onCreat