How:android开发中实现TextView垂直滚屏效果的方法

(本文所述方法是xml法)

实现该功能的模块组成:

1.layout,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ScrollView 
        android:id="@+id/scrollView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView 
        android:id="@+id/text_details"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:singleLine="false"
        android:textSize="20dp"/>
    </ScrollView>
</LinearLayout>
注意TextView标签包含在ScrollView标签内部

2.java类中,实现具体功能的代码

  public static class DetailsActivity extends Activity
    {
    	private TextView text;
    	@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.details);
			text = (TextView)findViewById(R.id.text_details);
			int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    4, this.getResources().getDisplayMetrics());
            text.setPadding(padding, padding, padding, padding);
			text.setText(Shakespeare.DIALOGUE[this.getIntent().getIntExtra("index", 0)]);
		}
    }

(另外)

如果要动态管理视图控件,则要在java 代码中实现,即此种情况xml发挥不了太大作用,但要注意,容器ScrollView和TextView不能一个在xml一个在代码中。


你可能感兴趣的:(How:android开发中实现TextView垂直滚屏效果的方法)