【问题标题】:How to set different themes for button selector?如何为按钮选择器设置不同的主题?
【发布时间】:2016-07-23 02:17:45
【问题描述】:

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

这是我的 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 或更高版本

标签: android button themes selector


【解决方案1】:

因为您的应用主题颜色位于 color.xml 中的 color_primary 中。你可以像这样在你的选择器中使用它。但是您必须创建两个可绘制文件,一个用于默认状态,另一个用于 selected_state。

button_selector.xml:

<?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);
                }
            }
        });

【讨论】:

  • 我只是想根据所选主题更改按钮按下的颜色。
  • 感谢您的澄清,您的实际问题是一些问题,但您已经提到在问题中使用样式。编辑了您的问题。请批准。
【解决方案2】:

在您的应用中使用动态选择器,以便您可以根据需要分配颜色。

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 替代品?
猜你喜欢
  • 1970-01-01
  • 2017-09-30
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多