我目前正在學習Android的一些技術,我想通過按鈕(通過Java)將TextViews添加到應用(View)中。但是在通過代碼添加之前,我想給每個TextView附加一個來自"res/values/styles.xml"的自制預配置樣式。這就是到目前為止的主要想法。
基本上,我想知道如何在活動的XML檔案中配置我的TextViews(在style.xml中)和給定的Layout(例如main_activity.xml),以便它們看起來像這樣的圖片:
因此,我們的目標是以這樣的方式預設布局和文本視圖,我只需要一個接一個地添加文本視圖,使它們以圖片中的方式對齊。
為了實作這個目標,我到底應該怎么做?
// main_activity.xml
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
...>/span>
<RelativeLayout。
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>>
<TextView style="@style/AddedTextView"/>
</RelativeLayout>
...
<Button ...
android:id="@ id/btn_add"
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:text="Button" />
</LinearLayout>
//res/values/styles.xml
<?xml version="1.0" encoding="utf-8"? >
<resources>/span>
<style name="AddedTextView" parent="Widget。 AppCompat.TextView">。
<item name="android: layout_width">wrap_content< /item>
<item name="android: layout_height">wrap_content</item>
<item name="android: background">@drawable/custom_textview_design</item>
<item name="android: text"/span>>TestTV</item>
<item name="android: textColor">@color/common_text_color</item>/span>
<item name="android: textSize">/span>7pt</item>
<item name="android: layout_alignParentLeft">true</item>
</style>>
</resources>
uj5u.com熱心網友回復:
你可以使用ContextThemeWrapper以編程方式應用該樣式:
val textView = TextView(ContextThemeWrapper(this, R.style.addedTextView)
而要添加相互連接的TextView,并將其垂直包裝,你可以使用Google的
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/313083.html
標籤:
下一篇:ImageView不能保持回應
