【问题标题】:How to change the selector/ripple color on a standard button?如何更改标准按钮上的选择器/波纹颜色?
【发布时间】:2015-11-04 01:47:20
【问题描述】:

我在布局中使用标准按钮:

<Button
       android:id="@+id/earlier_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="earlier"/>

我想出了如何在不改变默认样式的情况下改变它的颜色:

earlierButton = (Button) findViewById(R.id.earlier_button);
earlierButton.getBackground().setColorFilter(getResources().getColor(R.color.red_500), PorterDuff.Mode.MULTIPLY);

我现在的问题是,如何设置新的选择器/波纹或更改当前选择器的颜色?有谁知道需要为此覆盖的默认主题/样式?谈论这些按钮:

【问题讨论】:

    标签: android button themes selector


    【解决方案1】:

    你可以设置选择器。

    查看此链接了解详情。 https://stackoverflow.com/a/28431028/850347

    你有按钮,

    <Button
       android:id="@+id/earlier_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="earlier"/>
    

    你可能有 selecor,

    btn_selecor.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/btn_normal" android:state_pressed="false"/>
        <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
    
    </selector>
    
    • btn_normal, btn_pressed 是 png 图片。
    • 注意这个btn_selecor.xml必须放在文件夹名“drawable”

    最后,以编程方式将选择器设置为您的按钮。

        Drawable buttonDrawable = getResources().getDrawable(R.drawable.btn_selector);
        buttonDrawable.mutate();
        btn1.setBackground(buttonDrawable);
    


    编辑1: 当然,您可以使用下面的代码设置 android 默认按钮选择器。 在这种情况下,您不需要制作自己的选择器。

    Drawable buttonDrawable = getResources().getDrawable(android.R.drawable.btn_default);
    buttonDrawable.mutate();
    btn1.setBackground(buttonDrawable);
    

    【讨论】:

    • 感谢您的回答。实际上我想使用android提供的标准按钮样式而不是创建自己的png。是否有可能以 androids btn_pressed 作为父级设置自定义样式并更改其颜色?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多