ScrollView ScrollView ScrollView in Android through Java Code - Example of Lyrics Scroll

scene

The results are as follows

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Focus on Public Number
Domineering program ape
Get programming-related e-books, tutorial pushes, and free downloads.

Realization

Change the layout to LinearLayout and set it to vertical through android:orientation="vertical">then add the id property and set the inner margin

<?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>

Then open strings.xml under res to add string resources

<resources>
    <string name="app_name">RelativeLayoutTest</string>
    <string name="lyric">
        Public Number: Domineering Programming Ape\n
        Public Number: Domineering Programming Ape\n
        Public Number: Domineering Programming Ape\n
        Public Number: Domineering Programming Ape\n
        Public Number: Domineering Programming Ape\n
        Public Number: Domineering Programming Ape\n
        Public Number: Domineering Programming Ape\n
        On this windy battlefield\n
        Storm Boys Come o n n
        Overcoming the roar of the fire\n
        Noise all over the world\n
        The era of smoky signaling armour is approaching \nn
        Hot blood upstream\n
        Warriors are irresistible when their chariots are hot\n
        come on Anti-war and Counter-war Want to Be Wild\n
        Surge the Universe and Level the World\n
        Oh Anti-Battle Anti-Battle Wild Trump to Leave\n
        Fighting is our stubborn starting point\n
        I want to take control of my power\n
        Make my voice known\n
        See this dragon battle in the field\n
        Hundreds of warm-blooded soldiers in this battlefield\n
        Fly forward all the way\n
        Warriors defending the world\n
        Fighting another duel\n
        On this windy battlefield\n
        Storm Boys Come o n n
        Overcoming the roar of the fire\n
        Noise all over the world\n
        A signal of smoke flying \n
        The era of machine armour is coming\n
        Hot blood upstream\n
        The chariot is hot\n
        Warriors are unstoppable too\n
        come on Anti-war Counter-war Comes also\n
        King's card is wild\n
        Surge the Universe and Level the World\n
        Oh Anti-war Anti-war Wilderness\n
        Trump card to let go\n
        Fighting is our stubborn starting point\n
        I want to take control of my power\n
        Make my voice known\n
        See this dragon battle in the field\n
        Hundreds of warm-blooded soldiers in this battlefield\n
        Fly forward all the way\n
        Warriors defending the world\n
        Fighting another duel\n
        Brothers one by one\n
        Continue to be tough in the future\n
        Watch the flames fly\n
        Disrupt your opponent\n
        Bear and Bear Move Back\n
        Counter-war and Counter-war are coming too\n
        King's card is wild\n
        Surge the Universe and Level the World\n
        Oh Anti-war Anti-war Wilderness\n
        Trump card to let go\n
        Fighting is our stubborn starting point\n
        I want to take control of my power\n
        Make my voice known\n
        See this dragon battle in the field\n
        Hundreds of warm-blooded soldiers in this battlefield\n
        Fly forward all the way\n
        Warriors defending the world\n
        Fighting another duel\n
    </string>
</resources>

Then open the 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);
        //Obtain LinearLayout1
        LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1);
        //statement LinearLayout2
        LinearLayout ll2 = new LinearLayout(ScrollViewActivity.this);
        //Set layout direction vertical
        ll2.setOrientation(LinearLayout.VERTICAL);
        //Declare Scroll View
        ScrollView scrollView = new ScrollView(ScrollViewActivity.this);
        //Add Scroll View to LinearLayout1
        ll1.addView(scrollView);
        //take LinearLayout2 Add to Scroll View
        scrollView.addView(ll2);
        //statement ImagevView
        ImageView imageView = new ImageView(ScrollViewActivity.this);
        //Set up photos
        imageView.setImageResource(R.drawable.dog);
        //take ImageView add to LinearLayout2
        ll2.addView(imageView);
        //statement TextView
        TextView textView = new TextView(ScrollViewActivity.this);
        //Set up TextView Contents
        textView.setText(R.string.lyric);
        //take TextView add to LinearLayout
        ll2.addView(textView);
    }
}

 

Tags: Android Programming xml encoding

Posted on Tue, 07 Jan 2020 09:13:56 -0800 by cheekydump