滚动视图

在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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值