开发者

how to achieve transparency for an imagebutton in android

开发者 https://www.devze.com 2023-02-26 18:55 出处:网络
I want the imagebutton to appear transparently on the screen so i have made android:background=\"@null\" in the XML file so it has removed the gray border around the imagebutton.It solved my problem b

I want the imagebutton to appear transparently on the screen so i have made android:background="@null" in the XML file so it has removed the gray border around the imagebutton.It solved my problem but when i click on the imagebutton it is not showing any background color. But i need some background color to appear on click. So I have added android:padding="3dp". It has removed the border on the sides but not on the top.

<ImageButton  
android:id="@+id/btnphoto"  
android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
android:src="@drawable/photo"  
android:layout_x="4px"  
android:layout_y="370px"  
android:padding="3dp" /> 
开发者_JAVA技巧

Please help me.

Thanks in advance.


To change View aspect on state changes (click, press, focus, ...) you can use StateListDrawable as android:background. ColorStateList can be usefull too.

This post shows a good example of how to use.


android:background="@android:color/transparent"

or, if you want only some transparency, you can control it with the alpha channel.

Something like

android:background="#7F000000"

would be a partially transparent black.


You can try making your custum button background, using images or shapes in XML...

For example, this shape make one button state where the colors are transparent because they are ARGB type.

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="#EB363636" />
    <corners
        android:bottomRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
    <stroke
        android:width="2dip"
        android:color="#D1000000" />
</shape>

Then you can combine multiple shapes/images to make an button react the diferent states:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true">

    <item 
        android:state_window_focused="false"
        android:drawable="@android:color/transparent" />
    <item 
        android:state_pressed="true"
        android:drawable="@layout/shape_btn"/>
    <item 
        android:state_focused="true"
        android:drawable="@layout/shape_btn"/>
    <item
        android:drawable="@android:color/transparent"/>

</selector>

After making your selector you can use it as your button background using android:background="NAME OF SELECTOR FILE"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号