【问题标题】:Changing background of button via Selector通过 Selector 更改按钮的背景
【发布时间】:2013-10-05 08:54:56
【问题描述】:

我有一个按钮,我想在单击按钮时,按钮更改图像。我认为一切都很好,但是当点击按钮时,背景按钮改变了一会儿(当我选择这个时),然后背景按钮成为第一个背景颜色:(我的问题是什么??

contact_selector.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/contactpress"></item>
<item 
    android:state_focused="true"
    android:drawable="@drawable/contactpress"></item>
<item 
    android:drawable="@drawable/contact"></item>
</selector>

activity_test.xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout 
    android:id="@+id/frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:layout_above="@id/btn"></FrameLayout>
<Button 
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/contact_selector"
    android:layout_alignParentBottom="true"/>
 </RelativeLayout>

谢谢...

【问题讨论】:

    标签: android button selector


    【解决方案1】:
    // try this
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/contactpress" android:state_pressed="true"></item>
        <item android:drawable="@drawable/contactpress" android:state_focused="true"></item>
        <item android:drawable="@drawable/contact" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
        <item android:drawable="@drawable/contactpress" android:state_enabled="false"></item>
    
    </selector>
    

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多