gpt4 book ai didi

android - 如何使用最新的 AppCompat 为按钮着色

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:15 27 4
gpt4 key购买 nike

我需要知道使用最新的 AppCompat(目前为 23.2.1)为 Material Button (AppCompatButton) 着色的最佳(和推荐)方法是什么。我从来没有想过它会如此令人沮丧!我尝试了 here 中的大部分答案但它们要么不起作用,要么产生意想不到的结果。

我需要保持对 api >= 9 的向后兼容性并且只需要将链式 react 应用于 >=21 没什么特别的。那么到目前为止最好的方法是什么?

如果您能同时提供 xml 和 java 代码,我将不胜感激。

最佳答案

有很多方法可以做到这一点。我最喜欢的是以下内容:

<Button
android:id="@+id/activity_main_some_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Button.Colored"
android:text="This is a button" />

这会自动使用您(希望)在主题中设置的强调色为按钮着色,同时在 API < Lollipop and Ripple >= Lollipop 保持按下状态。

如果没有别的办法,你可以自己给按钮着色:

AppCompatButton myExampleButton = new AppCompatButton(getContext());

myExampleButton.setSupportBackgroundTintList(ContextCompat.getColorStateList(getContext(),
R.color.some_color));

更新

您可以执行以下操作以使用自定义颜色:

<style name="MyButtonTheme" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">@color/someColor</item>
</style>

定义具有所需颜色的新样式。

<Button
android:id="@+id/activity_main_some_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/MyButtonTheme"
android:text="This is a button" />

将其设置为您的按钮。

关于android - 如何使用最新的 AppCompat 为按钮着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36105094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com