android - appcompat-v7 自定义按钮颜色

标签 android xml android-appcompat

是否可以为 appcompat-v7 自定义按钮颜色?默认按钮是灰色的,我想在整个应用程序中将它们更改为绿色。将 android:background 设置为可绘制对象会移除弯曲的边缘。

最佳答案

由于您明确询问如何使用 appcompat-v7 执行此操作 - 这是推荐的方法。
首先:不需要创建新的可绘制对象。您可以通过使用具有 colorButtonNormal 属性的主题来简单地更改按钮的颜色。

下面是一个例子:

styles.xml

<style name="ThemeButton">
        <item name="colorButtonNormal">#009688</item>
</style>

layout.xml

<!-- other layout elements -->

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="My Button"
    android:textColor="@android:color/white"
    android:theme="@style/ThemeButton" />

结果:

button showing result

这样,您将在按下按钮时保持标准的高度和波纹效果,而使用自定义可绘制对象时则不会。 (至少如果你不使用选择器)

关于android - appcompat-v7 自定义按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31917012/

相关文章:

java - 使用java将xml转换为具有特定xml元素的excel

php - eBay API 设置子类别不起作用

.net - 使用 XmlTextReader 时设置 User-Agent

android - RecyclerView : java. lang.IndexOutOfBoundsException : Inconsistency detected. 项目位置无效

android - 检查两个 Bundle 对象在 Android 中是否相等?

android - 为什么使用绑定(bind)类的 View 引用标有@Nullable?

android - 在 Android 中的两个进程之间保留一个打开的套接字

java - 如何与 FragmentStatePagerAdapter 内的 Fragment 进行通信?

android - 添加媒体路由器转换按钮时出错

Android Studio Build error with AppCompat library newbie q