Android中通过Java代码实现ScrollView滚动视图-以歌词滚动为例

场景

实现效果如下

 

注:

博客:
https://blog.youkuaiyun.com/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

将布局改为LinearLayout,并通过android:orientation="vertical">设置为垂直布局,然后添加id属性,并设置内边距

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/ll1"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".ScrollViewActivity">

</LinearLayout>

然后打开res下strings.xml,添加字符串资源

<resources>
    <string name="app_name">RelativeLayoutTest</string>
    <string name="lyric">
        公众号:霸道的程序猿\n
        公众号:霸道的程序猿\n
        公众号:霸道的程序猿\n
        公众号:霸道的程序猿\n
        公众号:霸道的程序猿\n
        公众号:霸道的程序猿\n
        公众号:霸道的程序猿\n
        在这个风起云涌的战场上\n
        暴风少年登场\n
        在战胜烈火重重的咆哮声\n
        喧闹整个世界\n
        硝烟狂飞的讯号 机甲时代正来到\n\n
        热血逆流而上\n
        战车在发烫 勇士也势不可挡\n
        come on逆战 逆战来也 王牌要狂野\n
        闯荡宇宙摆平世界\n
        Oh 逆战 逆战狂野 王牌要发泄\n
        战斗是我们倔强起点\n
        我要操控我的权势\n
        张扬我的声势\n
        看这场龙战在野\n
        这战场千百热血战士\n
        一路向前飞驰\n
        捍卫世界的勇士\n
        Fighting 再一决\n
        在这个风起云涌的战场上\n
        暴风少年登场\n
        在战胜烈火重重的咆哮声\n
        喧闹整个世界\n
        硝烟狂飞的讯号\n
        机甲时代正来到\n
        热血逆流而上\n
        战车在发烫\n
        勇士也势不可挡\n
        come on逆战 逆战来也\n
        王牌要狂野\n
        闯荡宇宙摆平世界\n
        Oh 逆战 逆战狂野\n
        王牌要发泄\n
        战斗是我们倔强起点\n
        我要操控我的权势\n
        张扬我的声势\n
        看这场龙战在野\n
        这战场千百热血战士\n
        一路向前飞驰\n
        捍卫世界的勇士\n
        Fighting 再一决\n
        兄弟一场\n
        未来继续顽强\n
        看着战火飘摇\n
        瓦解对手力量\n
        熊熊气势再出发\n
        逆战 逆战来也\n
        王牌要狂野\n
        闯荡宇宙摆平世界\n
        Oh 逆战 逆战狂野\n
        王牌要发泄\n
        战斗是我们倔强起点\n
        我要操控我的权势\n
        张扬我的声势\n
        看这场龙战在野\n
        这战场千百热血战士\n
        一路向前飞驰\n
        捍卫世界的勇士\n
        Fighting 再一决\n
    </string>
</resources>

然后打开activity

package com.badao.relativelayouttest;

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 ScrollViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll_view);
        //获取LinearLayout1
        LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1);
        //声明LinearLayout2
        LinearLayout ll2 = new LinearLayout(ScrollViewActivity.this);
        //设置布局方向垂直
        ll2.setOrientation(LinearLayout.VERTICAL);
        //声明滚动视图
        ScrollView scrollView = new ScrollView(ScrollViewActivity.this);
        //将滚动视图添加到LinearLayout1
        ll1.addView(scrollView);
        //将LinearLayout2添加到滚动视图
        scrollView.addView(ll2);
        //声明ImagevView
        ImageView imageView = new ImageView(ScrollViewActivity.this);
        //设置照片
        imageView.setImageResource(R.drawable.dog);
        //将ImageView添加到LinearLayout2
        ll2.addView(imageView);
        //声明TextView
        TextView textView = new TextView(ScrollViewActivity.this);
        //设置TextView的内容
        textView.setText(R.string.lyric);
        //将TextView添加到LinearLayout
        ll2.addView(textView);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值