重用的TextView code在Android的XML [英] Reuse TextView code in Android XML

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

问题描述

我做了几个菜单为我的Andr​​oid应用和整个存在是重复5次,只改变了Android标准的TextView:每次文本标记,其他一切都是一样的。

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.

有这个特性的好一些,感觉非常低效是复制/粘贴这些都为每个textviews的。

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?

推荐答案

是的,你可以定义一个样式。在你的价值观res文件夹名称styles.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"/>

注意样式声明指上述定义的样式。样式几乎可以包含任何属性。 这里阅读上他们。

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

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