对于自定义视图主题风格 [英] Theme style for custom view

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

问题描述

我的应用程序有几个主题,从中用户可以选择(红,蓝,绿等),每个主题都有相应的按钮样式(RedTheme => RedButton)。 现在,我已经创建了一个自定义的视图,它稍微延伸按钮,不幸的是这个按钮并没有得到任何的风格,但我用的是风格属性的layout.xml。 我不使用任何附加属性,我只希望我的按钮的风格。 我想要做的比类似的事情:

My app has several themes from which the user can chose (red, blue, green etc.) Every theme has a corresponding button style (RedTheme => RedButton). Now I have created a custom view which slightly extends "Button" and unfortunately this button does not get any style, except I use the "style" attribute in a layout.xml. I don’t use any additional attributes, I just want that my Button style is applied. I want to do something similar than:

 <item name="android:imageButtonStyle">@style/ImageButtonmyTimeRed</item>

我已经看了很多其他职位像这样的: <一href="http://stackoverflow.com/questions/4493947/how-to-define-theme-style-item-for-custom-widget">How :定义主题(风格)项的自定义部件 但我不是100%肯定,如果这个解决方案适合我,因为我不需要任何额外的属性。

I have already read many other posts like this one: How to: Define theme (style) item for custom widget but I’m not 100% sure if this solution fits for me since I don’t need any additional attributes.

推荐答案

我用三个参数超级构造,并通过按钮样式资源ID。这适用于所有的自定义主题。感谢@pskink指着我到正确的方向。

I'm using the three parameter super constructor and pass the button style resource id. This works for all my custom themes. Thanks @pskink for pointing me into right direction.

public ClockButton(Context context, AttributeSet attrs) {
    super(context, attrs, R.attr.buttonStyle);
    ...
}

我的主题是这样的:

My theme looks like this:

<style name="LilaTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="buttonStyle">@style/ButtonmyTimeLila</item>
</style>

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

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