Disable pressed background color of button

倾然丶 夕夏残阳落幕 提交于 2019-12-10 12:12:54

问题


My button consists of only letters. Every time i press it, it has a black background. How do i disable the black background? I don´t want to change the color, i want to disable the pressed state.


回答1:


You need to create selector under drawable folder and set color or image over there for different sates of the button.

I have created one sample application for you and am pasting code snippet:

Code for button_selector.xml that contains selector code for button which you will need to put under drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/no_box_selector" android:state_pressed="true"></item>
    <item android:drawable="@drawable/no_box"></item>

</selector>

You will need to put no_box_selector and no_box pngs in drawable-hdpi or any drawable folders

Code for activity_main.xml that will be your main xml that contains only one button as of now:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relRingtone"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/button_selector"
        android:padding="5dp"
        android:text="Click Me"
        android:textColor="#FFF"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

Try implementing these things and let me know if you need any further assisstance.....

All the best!!!




回答2:


Make a selector in drarable folder. It will be look like

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

Set this selector as a backgroung to the button. Now you can change in selector for all the states of button.



来源:https://stackoverflow.com/questions/18227827/disable-pressed-background-color-of-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!