您的位置:首页 > 移动开发 > Android开发

为文本框添加滚动条

2015-11-11 13:08 435 查看
可以在xml文件中定义布局,也可以使用代码的方式动态添加布局。下面的例子采用后者的方式:

为文本框添加滚动条,就是在ScrollView 中添加TextView。ScrollView 是一个滚动视图,它只能包含一个子视图,如果需要滚动多个视图,可以让ScrollView 包含一个Layout。

在java 代码中,在onCreate() 方法中新建视图并向其传递上下文参数this,调用addView 方法可以向容器视图添加子视图:

RelativeLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.layout);
ScrollView scrollView = new ScrollView(this);
TextView text = new TextView(this);
text.setPadding(10, 10, 10, 10);
text.setTextSize(18);
text.setText(R.string.text);
scrollView.addView(text);
layout.addView(scrollView);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android开发 TextView