場景
實作效果如下
注:
博客:
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); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/39290.html
標籤:Android
