我將重復以下代碼中的 10 個,但我詢問如何縮短以下代碼?有沒有一種簡單的方法可以重復此代碼而無需全部輸入?
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text=" view 1 "
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="36dp" />
uj5u.com熱心網友回復:
定義一個包含所有通用屬性的樣式并在textview. 在您的情況下,樣式可能包含 , textAlignment, textColor, textSizeand if you want layout_widthand layout_widthalso 。如果您只想更改一個的值textview,您始終可以在 xml 檔案中覆寫該引數。
<style name="TextCommanStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/black</item>
<item name="android:textSize">20sp</item>
</style>
你的 xml 檔案。
<TextView
android:id="@ id/titleTextView"
style="@style/TextCommanStyle"
tools:text="Text 1" />
<TextView
android:id="@ id/descTextView"
style="@style/TextCommanStyle"
android:layout_marginTop="10dp"
tools:text="Text 2" />
<TextView
android:id="@ id/desc2TextView"
style="@style/TextCommanStyle"
android:layout_marginTop="10dp"
android:textColor="@color/teal_200" // override color if you want
tools:text="Text 3" />
如果您想為textColor每個 textview 更改或添加新屬性,請添加到 commom 樣式,它將反映使用該樣式的所有視圖。
您可以根據您的用例創建多種樣式
uj5u.com熱心網友回復:
使用 aninclude重用布局而不復制它:
https://developer.android.com/training/improving-layouts/reusing-layouts
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415027.html
標籤:
上一篇:如何在AlertDialog中的CardView上放置onClick
下一篇:將XML節點輸出到單個檔案中
