Using Image in a Android Button with effects

无人久伴 提交于 2020-01-02 02:43:10

问题


(Now I have come across related questions on StackOverflow but unfortunately none of the solutions worked for me which is why I had to ask this separately)

I am a Novice to Android. The problem: I need to have an image that acts as a button. Now I understand that this can be achieved by either using an image on a standard button or by using something called as "ImageButton" (which I guess is highly recommended although I have no idea why).

Requirements: I need detailed guidance for going about this problem. Specifically, I thought the "putting-image-on-standard-button" was easier until I faced two major issues: I was unable to set the image in the center (thanks drawable-top,bottom,left6,right) and once I changed the background color to match that of the Activity screen's back-color, the button effect disappeared. Simply put, I need a moderately easy way of having an image act as a button or a button with an image which has all three effects: focussed, pressed and default. I used ImageButton but then I did not know how to make custom shapes or 9patch images to give it all the desired effects, I am pretty satisfied with the default button that android provided. All I simply need is something like a background hover over the image or something of that sort which indicates the user that the image is being pressed and the event has been generated!

Can someone please help me out with this? I need the UI to look decent and therefore, need the corresponding effects on my image/button. Thanks in Advance :)

This is my icon-image:

I wish to have some sort of hover effect around this that indicates that the image has been pressed just like any normal button.


回答1:


Use ImageButton and StateList Drawable. You need selector for different button's states. You can assign different drawable for different state to imitate the onFocus or onPressed effect on normal button.

This is selector.xml in drawable folder under res:

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
      android:drawable="@color/cyan"/> <!-- pressed state -->
<item android:state_focused="true" 
      android:drawable="@color/cyan"/> <!-- focused state -->
<item android:drawable="@android:color/transparent"/> <!-- default state -->

</selector>

And this is color.xml in values folder under res:

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <color name="cyan">#33B5E5</color>
</resources>

Set the ImageButton's src to your image and set the background to selector.xml.

This is the final result:

There is a good tutorial here: Android ImageButton Selector Example




回答2:


create xml view

<ImageView
 android:id="@+id/imageview1"
 android:background="@drawable/selector_xml_name"
 android:layout_width="200dp"
 android:layout_height="126dp"/>

create inside draw able folder selector_xml_name.xml

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

create inside draw able folder numpad_button_bg_selected.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
 <solid android:color="@color/selected"/>
 <padding />
 <stroke android:color="#000" android:width="1dp"/>
 <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>




回答3:


If someone also still has an issue with this. I've created the selector but referred the drawable to two different image files, and used the XML in the imagebutton as a source. It worked like a charm.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_add_pressed"
        android:state_pressed="true" />
    <item android:drawable="@drawable/btn_add"
        android:state_focused="true" />
    <item android:drawable="@drawable/btn_add" />
</selector>

And the image button looks like this:

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/add_button_selector"
        android:background="@null"/>


来源:https://stackoverflow.com/questions/17145615/using-image-in-a-android-button-with-effects

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