欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新

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

来源: 开发者 投稿于  被查看 36941 次 评论:50

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


场景

实现效果如下

 

 

注:

博客:
https://blog.csdn.net/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);
    }
}

 

相关文章

    暂无相关文章

用户评论