在xml中添加滚动视图
垂直方向上滚动
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:textSize="25sp"
/>
</ScrollView>
</RelativeLayout>
水平方向上滚动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:textSize="25sp"
/>
</HorizontalScrollView>
</RelativeLayout>
在滚动视图中放置多个组件
在一个滚动视图中只能放置一个组件,如果想要放置多个,需要使用布局管理器,将多个组件包括起来:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:textSize="25sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:textSize="25sp"
/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
在Java文件中添加
- 1、使用构造方法ScrollView(Context c)创建一个滚动视图
- 2、应用addView()方法添加组件到滚动视图中
- 3、将滚动视图添加到布局管理器中
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll"
tools:context=".MainActivity">
</LinearLayout>
java:
package com.example.scrollview;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建布局管理器
LinearLayout linearLayout=findViewById(R.id.ll);
LinearLayout linearLayout1=new LinearLayout(MainActivity.this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
//将滚动视图添加到布局管理器中
ScrollView scrollView=new ScrollView(MainActivity.this);
linearLayout.addView(scrollView);
scrollView.addView(linearLayout1);
//创建图片视图
ImageView imageView=new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.cloud);
linearLayout1.addView(imageView);
TextView textView=new TextView(MainActivity.this);
textView.setText(R.string.content);
linearLayout1.addView(textView);
}
}