Java Android:使用 XML 为切换按钮指定两个不同的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1533038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:54:22  来源:igfitidea点击:

Android: Specify two different images for togglebutton using XML

javaandroidxmltogglebutton

提问by I82Much

I'm attempting to override the default ToggleButtonappearance. Here's the XML that defines the ToggleButton:

我正在尝试覆盖默认ToggleButton外观。这是定义 的 XML ToggleButton

<ToggleButton android:id="@+id/FollowAndCenterButton"
        android:layout_width="30px"
        android:layout_height="30px"
        android:textOn="" android:textOff="" android:layout_alignParentLeft="true"
        android:layout_marginLeft="5px"
        android:layout_marginTop="5px" android:background="@drawable/locate_me"/>

Now, we have two 30 x 30 icons we want to use for the clicked/non-clicked states. Right now we have code that programmatically changes the background icon depending on the state:

现在,我们有两个 30 x 30 的图标要用于单击/非单击状态。现在我们有代码可以根据状态以编程方式更改背景图标:

centeredOnLocation.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (centeredOnLocation.isChecked()) {
                centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on));
            } else {
                centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me));
            }
        }
});

Obviously I'm looking for a better way to do this. I've tried to make a selector for the background image, which would automatically switch between the states:

显然,我正在寻找一种更好的方法来做到这一点。我试图为背景图像制作一个选择器,它会自动在状态之间切换:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@drawable/locate_me" /> <!-- default -->
 <item android:state_checked="true"
       android:drawable="@drawable/locate_me_on" /> <!-- pressed -->
 <item android:state_checked="false"
       android:drawable="@drawable/locate_me" /> <!-- unchecked -->

But this does not work; reading the ToggleButtonAPI (http://developer.android.com/reference/android/widget/ToggleButton.html), it appears that the only inherited xml attributes are

但这不起作用;阅读ToggleButtonAPI(http://developer.android.com/reference/android/widget/ToggleButton.html),似乎唯一继承的xml属性是

    XML Attributes
Attribute Name  Related Method  Description
android:disabledAlpha       The alpha to apply to the indicator when disabled. 
android:textOff         The text for the button when it is not checked. 
android:textOn      The text for the button when it is checked. 

There does not seem to be the android:state_checked attribute, despite the class having the method isChecked()and setChecked().

尽管该类具有方法isChecked()和,但似乎没有 android:state_checked 属性setChecked()

So, is there a way to do what I want in XML, or am I stuck with my messy workaround?

那么,有没有办法在 XML 中做我想做的事情,或者我是否坚持我的凌乱解决方法?

采纳答案by m_vitaly

Your code is fine. However, the toggle button will display the first item in your selector that it matches, so the default should come last. Arrange the items in the following manner to ensure they will all be utilized:

你的代码没问题。但是,切换按钮将显示选择器中匹配的第一个项目,因此默认值应该放在最后。按以下方式排列物品以确保它们都将被利用:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on
    <item android:state_pressed="true" /> //currently pressed turning the toggle off
    <item android:state_checked="true" /> //not pressed default checked state
    <item /> //default non-pressed non-checked
</selector>