ScrollView(滚动条)
.滚动到底部:
我们可以直接利用ScrollView给我们提供的:fullScroll()方法:
scrollView.fullScroll(ScrollView.FOCUS_DOWN);滚动到底部
scrollView.fullScroll(ScrollView.FOCUS_UP);滚动到顶部
另外用这玩意的时候要小心异步的玩意,就是addView后,有可能还没有显示完, 如果这个时候直接调用该方法的话,可能会无效,这就需要自己写handler来更新了~
代码示例:
效果图:
实现代码:
布局比较简单,就不贴了,直接贴MainActivity MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_down;
private Button btn_up;
private ScrollView scrollView;
private TextView txt_show;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
}
private void bindViews() {
btn_down = (Button) findViewById(R.id.btn_down);
btn_up = (Button) findViewById(R.id.btn_up);
scrollView = (ScrollView) findViewById(R.id.scrollView);
txt_show = (TextView) findViewById(R.id.txt_show);
btn_down.setOnClickListener(this);
btn_up.setOnClickListener(this);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 100; i++) {
sb.append("呵呵 * " + i + "
");
}
txt_show.setText(sb.toString());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_down:
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
break;
case R.