在 Android XML 中重用 TextView 代码 [英] Reuse TextView code in Android XML

查看:29
本文介绍了在 Android XML 中重用 TextView 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 Android 应用制作一些菜单,整个过程中有一个重复 5 次的标准文本视图,每次只更改 android:text 标签,其他一切都相同.

I'm making out a few menus for my Android App and throughout there is a standard textview that is repeated 5 times, only changing the android:text tag each time, everything else is the same.

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

There are a good number of properties on this and it feels very inefficient to be copy/pasting all these for each of the textviews.

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

Is there a way I define the common properties just once and add them to each TextView element?

推荐答案

是的,您可以定义样式.在您的 values res 文件夹中创建一个名为 style.xml 的文件并添加如下内容:

Yes you can define a style. Create a file in your values res folder names styles.xml and add something like this:

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

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

This defines the style. In your layout you might have a field like this:

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

请注意,样式声明是指上面定义的样式.样式几乎可以包含任何属性.在此处阅读它们.

Notice the style statement refers to the style defined above. Styles can contain almost any property. Read up on them here.

这篇关于在 Android XML 中重用 TextView 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆