ScrollView是Android提供的垂直滑动的组件,HorizontalScrollView与之对应是水平滑动的组件,内部只能包含一个根布局,否则会报错。
报错log
ScrollView中提供了fullScroll来滚动到的位置,有如下4个常用位置:
ScrollView.FOCUS_DOWN;//底部
ScrollView.FOCUS_UP;//顶部
ScrollView.FOCUS_LEFT;//左边
ScrollView.FOCUS_RIGHT;//右边
效果图如下
滚动效果
这里布局文件使用了4个按钮和1个HorizontalScrollView、1个ScrollView,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/down_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="滚动到底部" />
<Button
android:id="@+id/up_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="滚动到顶部" />
<Button
android:id="@+id/left_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="滚动到左边" />
<Button
android:id="@+id/right_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="滚动到右边" />
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="30dp">
<LinearLayout
android:id="@+id/horizontal_root_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/root_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
MainActivity代码:
public class MainActivity extends AppCompatActivity {
private Button down_btn;
private Button up_btn;
private Button left_btn;
private Button right_btn;
private ScrollView scrollView;
private LinearLayout rootLl;
private HorizontalScrollView horizontalScrollView;
private LinearLayout horizontalRootLl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
down_btn = findViewById(R.id.down_btn);
up_btn = findViewById(R.id.up_btn);
left_btn = findViewById(R.id.left_btn);
right_btn = findViewById(R.id.right_btn);
scrollView = findViewById(R.id.scrollView);
horizontalScrollView = findViewById(R.id.horizontalScrollView);
horizontalRootLl = findViewById(R.id.horizontal_root_ll);
rootLl = findViewById(R.id.root_ll);
TextView tv;
for (int i = 0; i < 100; i++) {
tv = new TextView(this);
tv.setText("第" + i + "个");
rootLl.addView(tv);
}
for (int i = 0; i < 100; i++) {
tv = new TextView(this);
tv.setText("第" + i + "个");
horizontalRootLl.addView(tv);
}
down_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
up_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.fullScroll(ScrollView.FOCUS_UP);
}
});
left_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
horizontalScrollView.fullScroll(ScrollView.FOCUS_LEFT);
}
});
right_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
horizontalScrollView.fullScroll(ScrollView.FOCUS_RIGHT);
}
});
}
}
网友评论