Android ScrollView
时间:2020-02-23 14:29:15 来源:igfitidea点击:
Android ScrollView允许我们在android屏幕上创建可滚动的布局。
Android ScrollView
ScrollView是FrameLayout的一种特殊类型,它允许用户在视图列表中滚动。
当布局所占空间大于物理显示器时,此功能很有用。
ScrollView只能包含一个子视图或者ViewGroup,通常为LinearLayout。
注意:不要将ListView与ScrollView一起使用。
ListView旨在显示相关信息的列表,并已针对处理大型列表进行了优化。
另外,ListView包含内置的滚动功能。
ScrollView仅支持垂直滚动。
我们必须使用" HorizontalScrollView"进行水平滚动。android:fillViewport属性定义了滚动视图是否应拉伸其内容以填充视口。
ScrollView XML布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/widget54"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android"
>
<LinearLayout
android:layout_width="310px"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Back"
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1"
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2"
<TextView
android:text="This textView is placed inside
a LinearLayout which is a child ViewGroup
contained in a ScrollView"
android:layout_width="fill_parent"
android:textSize="18sp"
android:layout_height="750px"
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3"
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4"
</LinearLayout>
</ScrollView>
我们已经明确分配了较大的TextView的layout_height大小,以便使用ScrollView。
Android ScrollView示例项目
该项目包含两个活动:MainActivity和SecondActivity。
MainActivity显示TableLayout。
package com.theitroad.layoutspartthree;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_table);
next=(Button)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
@Override
public void onBackPressed() {
finish();
}
}
SecondActivity.java包含ScrollView及其内部的LinearLayout。
package com.theitroad.layoutspartthree;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends Activity {
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scroll);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}

