如何自定义AppCompat材质按钮样式? [英] How to Customize AppCompat Material Button Style?

查看:420
本文介绍了如何自定义AppCompat材质按钮样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是AppCompat的主题,我想设置属性了minHeight我的按钮:

I am using the AppCompat theme and I want set the minHeight attribute on my buttons:

<style name="Theme.MyTheme" parent="Theme.AppCompat">
    <item name="android:buttonStyle">@style/MyButtonStyle</item>
</style>

<style name="MyButtonStyle" parent="...?">
    <item name="android:minHeight">60dp</item>
</style>

然而,没有 Widget.AppCompat.Button 样式设置为父 MyButtonStyle 。如果我使用安卓Widget.Button ,那么我所有的按钮看起来像旧的糟糕的风格。我尝试了各种其他AppCompat主题,如 TextAppearance.AppCompat.Button ,但他们没有工作。

However, there is no Widget.AppCompat.Button style to set as the parent for MyButtonStyle. If I use android:Widget.Button, then all my buttons look like the old crappy style. I tried various other AppCompat themes like TextAppearance.AppCompat.Button, but they do not work.

离开了父主题为按钮样式也使按钮不被正确称呼。

Leaving out a parent theme for the button style also causes the button not to be styled correctly.

我如何自定义默认 Theme.AppCompat 按钮样式

推荐答案

可以有 Base.MyButtonStyle 延长安卓Widget.Holo.Button 的API 14+(在 RES /值-V14 / styles.xml )和安卓Widget.Material.Button 的API 21+(在 RES /值-V21 / styles.xml ,根据设备的系统版本这种风格会发生变化。把你的平台这里具体的修改。

You can have Base.MyButtonStyle extend android:Widget.Holo.Button on API 14+ (in res/values-v14/styles.xml) and android:Widget.Material.Button on API 21+ (in res/values-v21/styles.xml. This style will change according to the device system version. Put your platform specific modifications here.

然后让 MyButtonStyle 延长 Base.MyButtonStyle 和定义安卓了minHeight 这里( RES /价值/ styles.xml )。这将适用于所有平台。

Then have MyButtonStyle extend Base.MyButtonStyle and define the android:minHeight here (in res/values/styles.xml). This will apply to all platforms.

您的按钮,然后可以使用样式 MyButtonStyle

You buttons then can use style MyButtonStyle.

这个例子假设你的最小SDK是14。

This example assumes your minimum SDK is 14.

是的,有没有appcompat-V7按钮样式(好,至少目前还没有)。

And yes, there's no appcompat-v7 button style (well, at least not yet).

修改

此假设你与河洛OK按钮的平台上超过棒棒糖。感觉不显眼的,如果你能没有涟漪做,这应该只是罚款。如果你想涟漪,我建议你谷歌的第三方棒棒糖按钮库。

This assumes you're OK with Holo button on platforms older than Lollipop. It feels unobtrusive and if you can do without ripples, it should be just fine. If you want ripples I suggest you google for a third party lollipop button library.

这篇关于如何自定义AppCompat材质按钮样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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