单击图像按钮时 Selector.xml 不起作用

发布于 2024-12-11 21:01:19 字数 850 浏览 0 评论 0原文

当我单击它时,我使用以下选择器来更改图像按钮的图像。但这在大多数情况下不起作用。仅适用于少数按钮。不知道这里的原因是什么

 mapselector.xml
 <?xml version="1.0" encoding="utf-8"?>

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Button Pressed -->
        <item android:state_focused="true" android:state_pressed="true" 
    android:drawable="@drawable/mapiconover"/>
</selector>

,在按钮中,

我这样做,

  <ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/btnMap" android:layout_centerInParent="true"  android:src="@drawable/mapselector"
    android:background="@drawable/map" android:scaleType="fitXY"
    android:layout_alignParentBottom="true">
</ImageButton>

但当我单击时,静止图像没有改变。

对此有什么猜测吗?

I use the following selector to change image of image button, when I click on it. But this does not work most of the times. Only for few buttons it works. Don't know what the cause here is

 mapselector.xml
 <?xml version="1.0" encoding="utf-8"?>

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Button Pressed -->
        <item android:state_focused="true" android:state_pressed="true" 
    android:drawable="@drawable/mapiconover"/>
</selector>

And in the button,

Am doing like this,

  <ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/btnMap" android:layout_centerInParent="true"  android:src="@drawable/mapselector"
    android:background="@drawable/map" android:scaleType="fitXY"
    android:layout_alignParentBottom="true">
</ImageButton>

but still image is not changing when I click.

Any guess for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女净妖师 2024-12-18 21:01:19

您已经在 ImageButton xml 中创建了这样的内容,

使用按钮图像创建 xml 文件,如下所示

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item 
    android:state_pressed="true" 
    android:drawable="@drawable/backbutton" />
  <item 
   android:drawable="@drawable/closebutton" />
</selector>   

将该 xml 文件添加为 IMageButton 的背景

<ImageButton                 
    android:layout_height="50px" 
    android:layout_width="50px" 
    android:id="@+id/settings" 
    android:background="@drawable/settings_button" //setting_button in
                                                         the xml file            
    android:text="Settings"/>

you have create like this in the ImageButton xml

create xml file using the button image like this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item 
    android:state_pressed="true" 
    android:drawable="@drawable/backbutton" />
  <item 
   android:drawable="@drawable/closebutton" />
</selector>   

add that xml file as background for IMageButton

<ImageButton                 
    android:layout_height="50px" 
    android:layout_width="50px" 
    android:id="@+id/settings" 
    android:background="@drawable/settings_button" //setting_button in
                                                         the xml file            
    android:text="Settings"/>
梦年海沫深 2024-12-18 21:01:19

不确定这是否有效,但您可以尝试将选择器更改为:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Button Pressed -->
    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/mapiconover"/>
    <item android:drawable="@drawable/map" />

</selector>

将图像按钮更改为:

  <ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/btnMap" 
    android:layout_centerInParent="true"  
     android:background="@drawable/mapselector"
     android:scaleType="fitXY"
    android:layout_alignParentBottom="true">
</ImageButton>

Not sure if this will work but you could try changing your selector to this:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Button Pressed -->
    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/mapiconover"/>
    <item android:drawable="@drawable/map" />

</selector>

And your image button to this:

  <ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/btnMap" 
    android:layout_centerInParent="true"  
     android:background="@drawable/mapselector"
     android:scaleType="fitXY"
    android:layout_alignParentBottom="true">
</ImageButton>
緦唸λ蓇 2024-12-18 21:01:19

我也有同样的问题。对我来说是因为

android:state_xxxx

冲突。它通过澄清启用和按下之间的条件来解决。我使用以下代码来获取按下状态和正常状态之间的不同状态:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="false" >
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#1094ff"
                android:endColor="#7eb6e3"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#505050" />
        </shape>
    </item>
    <item android:state_pressed="true" android:state_enabled="true">
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#0075d4"
                android:endColor="#0070cb"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#ff0000" />
        </shape>
    </item>
    <item android:state_enabled="false" >
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#b9b9b9"
                android:endColor="#848484"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#505050" />
        </shape>
    </item>
</selector>

此示例对不同状态使用不同的渐变和笔划。您也可以使用您的可绘制对象来代替。只需在您的可绘制文件夹中创建一个类似“mybutton.xml”的文件即可。然后像这样设置按钮的背景属性:

android:background="@drawable/mybutton"

我希望它有帮助。

I had the same problem. For me it was because of

android:state_xxxx

conflict. It solved by clarifying the conditions between enabled and pressed. I used the following code to get different states between press and normal state:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="false" >
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#1094ff"
                android:endColor="#7eb6e3"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#505050" />
        </shape>
    </item>
    <item android:state_pressed="true" android:state_enabled="true">
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#0075d4"
                android:endColor="#0070cb"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#ff0000" />
        </shape>
    </item>
    <item android:state_enabled="false" >
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#b9b9b9"
                android:endColor="#848484"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#505050" />
        </shape>
    </item>
</selector>

This example uses different gradient and stroke for different states. You can also use your drawable instead. Just create a file like "mybutton.xml" in your drawable folder. then set the background property of your button like this:

android:background="@drawable/mybutton"

I hope it be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文