首页 技术 正文
技术 2022年11月15日
0 收藏 758 点赞 2,966 浏览 2933 个字

ScollView应用展示

在xml文件中添加滚动视图

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/content"
android:textSize="20sp" />
</ScrollView></RelativeLayout>

效果:

这样就添加了一条垂直的滚动条。

如果要水平的滚动条用HorizontalScrollView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:textSize="20sp" />
</HorizontalScrollView></RelativeLayout>

效果

通过Java文件创建滚动视图

步骤:

activity_main.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"
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"></LinearLayout>

就一个LinearLayout给一个id:ll

在string.xml资源文件里写点内容

<resources>
<string name="app_name">ScrollView2</string>
<string name="directory">
《龙族》是江南所著系列长篇魔幻小说,已出版《龙族1火之晨曦》《龙族2悼亡者之瞳》《龙族3黑月之潮(上、中、下)》《龙族4奥丁之渊》,《龙族5悼亡者的归来》2018年5月开始连载。《龙族》讲叙了少年路明非在申请国外大学时收到了来自芝加哥远郊处的一所私立大学——卡塞尔学院的邀请函,随着路明非同学坐上去往芝加哥的CC1000次列车,同时也踏上了与龙族争锋的征程。
.;..
章节列表
龙族1火之晨曦
开篇序章:白帝城第一幕 卡塞尔之门
一第一幕 卡塞尔之门 ·
二第一幕 卡塞尔之门 ·
三第一幕 卡塞尔之门 ·
.... </string>
</resources>

MainActivity.java

package com.example.scrollview2;import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
LinearLayout ll2 = new LinearLayout(MainActivity.this);
ll2.setOrientation(LinearLayout.VERTICAL);
ScrollView scrollView = new ScrollView(MainActivity.this);
ll.addView(scrollView);
scrollView.addView(ll2);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.peargril);
ll2.addView(imageView);
TextView textView = new TextView(MainActivity.this);
textView.setText(R.string.directory);
ll2.addView(textView);
}
}

效果:

上一篇: FR6安装问题备注
下一篇: DNS bind使用
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849