我正在为我的Android应用程序制作一些菜单,并且整个标准文本视图重复5次,每次只更改android:text标签,其他一切都是相同的.
这有很多属性,对于每个textview来复制/粘贴所有这些属性效率非常低.
有没有一种方法我只定义一次公共属性并将它们添加到每个TextView元素?
dha*_*g23 20
是的,你可以定义一种风格.在您的值res文件夹名称styles.xml中创建一个文件,并添加如下内容:
<resources>
<style name="my_header_text">
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">@android:color/white</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这定义了风格.在您的布局中,您可能有这样的字段:
<TextView
android:id="@+id/my_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/my_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="5dip"/>
Run Code Online (Sandbox Code Playgroud)
请注意,style语句引用上面定义的样式.样式几乎可以包含任何属性.在这里阅读它们.