如何为按钮选择器设置不同的主题?

5

如何根据当前应用程序主题设置不同的按钮选择器样式?

这是我的button_selector.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <color android:color="@color/color_theme1"/>
        </item>
 <!-- pressed -->
    <item android:drawable="@color/transparent"/>
 <!-- default -->

</selector>

显而易见的答案是使用实际的主题颜色而不是@color/color_theme1,例如像?colorPrimary。话虽如此,这只适用于API 21或更高版本。 - David Medenjak
2个回答

3
作为您的应用程序主题颜色在color.xml中是color_primary。你可以在选择器中使用它,但你需要创建两个drawable文件,一个用于默认状态,另一个用于选中状态。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--selected/pressed/focused -->
<item       android:state_selected="true"
            android:drawable="@drawable/button_selected"
            />
    <item android:drawable="@drawable/button_default"/>
 <!-- default -->

</selector>

button_default.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!--this is to give gradient effect -->
<gradient   android:angle="270"
               android:startColor="@color/gray"
               android:endColor="#@color/gray"
               />
<!-- this will make corners of button rounded -->
<corners    android:topLeftRadius="5dip"
               android:bottomRightRadius="5dip"
               android:topRightRadius="5dip"
               android:bottomLeftRadius="5dip"/>

</shape>

button_selected.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!--this is to give gradient effect -->
<gradient   android:angle="270"
            android:startColor="@color/color_primary"
            android:endColor="#@color/color_primary"
            />
<!-- this wil make corners of button rounded -->
<corners    android:topLeftRadius="5dip"
               android:bottomRightRadius="5dip"
               android:topRightRadius="5dip"
               android:bottomLeftRadius="5dip"/>

</shape>

您还需以编程方式执行以下操作,以使按钮保持选定状态。
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.isSelected())
                {
                    v.setSelected(false);
                }
                else
                {
                     v.setSelected(true);
                }
            }
        });

我只想根据所选主题更改按钮按下时的颜色。 - Sujith S Manjavana
谢谢您的澄清,您实际上的问题是另外一件事,但您在问题中提到了使用样式。我已编辑了您的问题,请批准。 - HourGlass

1
使用动态选择器来为您的应用程序分配颜色,以便根据您的要求进行调整。
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
    getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
    getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);

或者

检查thisthis是否能帮助您


有没有XML的替代方案? - Sujith S Manjavana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接