安卓:指定两个不同的图像的切换按钮使用XML按钮、图像、不同、两个

2023-09-12 00:40:43 作者:怪你过分美丽

我试图覆盖默认切换按钮的外观。下面是定义XML的切换按钮

I'm attempting to override the default ToggleButton appearance. Here's the XML that defines the 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"/>

现在,我们有我们想要使用的点击/非点击的状态的两个30×30的图标。现在,我们有code以编程方式改变根据国家的背景图标:

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:

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 -->

但是,这并不正常工作;读切换按钮 API(http://developer.android.com/reference/android/widget/ToggleButton.html),看来,既继承了XML属性是

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

    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. 

似乎没有成为机器人:state_checked属性,尽管有方法器isChecked() setChecked()。

那么,有没有办法做我想做的XML,还是我坚持我凌乱的解决方法吗?

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

推荐答案

您code是罚款。然而,切换按钮就会显示在你选择的第一项匹配,所以默认应该放在最后。安排在下述方式中的项目,以确保它们将全部被利用:

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>