【问题标题】:Reuse TextView code in Android XML在 Android XML 中重用 TextView 代码
【发布时间】:2011-05-05 21:40:04
【问题描述】:

我正在为我的 Android 应用程序制作一些菜单,并且整个过程中有一个标准的 textview 重复 5 次,每次只更改 android:text 标签,其他一切都是一样的。

这方面有很多属性,为每个文本视图复制/粘贴所有这些属性感觉非常低效。

有没有办法只定义一次公共属性并将它们添加到每个 TextView 元素?

【问题讨论】:

    标签: android xml menu


    【解决方案1】:

    是的,您可以定义样式。在您的值 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>
    

    这定义了样式。在您的布局中,您可能有这样的字段:

    <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"/>
    

    注意样式声明是指上面定义的样式。样式几乎可以包含任何属性。 Read up on them here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多