Android:自定义视图的默认属性 [英] Android: Default attributes for custom views

查看:100
本文介绍了Android:自定义视图的默认属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展框架类之一的自定义视图. Android中大多数View都有一些为其定义的默认属性(例如Button是可单击的,由android:clickable="true"设置).

I have a custom view that extends one of the framework classes. Most Views in Android have some default attributes defined for them (such as Button being clickable, which is set by android:clickable="true").

如何为自定义视图提供应用程序范围的默认值?

How do I provide application-wide defaults for my custom view?

推荐答案

我因此解决了自己的问题:

I solved my own problem thusly:

res/values/attrs.xml :

<resources>
    <attr name="customViewStyle" type="reference" />
</resources>

res/values/styles.xml :

<resources>
    <style name="AppTheme" parent="@android:style/Theme.Light">
        <item name="android:background">@drawable/bg_light</item>
        <item name="customViewStyle">@style/CustomView</item>
    </style>

    <style name="CustomView">
        <item name="android:clickable">true</item>
        <item name="android:focusable">true</item>
        <item name="android:focusableInTouchMode">true</item>
    </style>
</resources>

然后,您要做的就是将清单中的主题设置为AppTheme.这也适用于使用android:[widget]Style代替AppTheme定义中的customViewStyle的标准小部件.

Then all you do is set the theme in the manifest to AppTheme. This also works with standard widgets using android:[widget]Style in place of customViewStyle in the definition of AppTheme.

这篇关于Android:自定义视图的默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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