[英]i will repeat 10 of the following code but i ask for How do I shorten the following code?
我將重復以下代碼中的 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" />
在textview
中定義包含所有公共屬性的樣式並設置樣式。 在您的情況下,樣式可能包含textAlignment
、 textColor
、 textSize
,如果您還想要layout_width
和layout_width
。 如果您只想更改一個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 添加新屬性,請添加到通用樣式,它將反映使用該樣式的所有視圖。
您可以根據您的用例創建多個 styles
使用include
重用布局而不復制它:
https://developer.android.com/training/improving-layouts/reusing-layouts
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.