How to create a transparent Button?

2019-06-22 18:07发布

问题:

I am trying to create a transparent Button. In details I set the background's button with an image and I would like to modify the transparency of the button in order to see the background layout's color.

回答1:

you can use setAlpha() method to your Button.

Usingbutton.setAlpha(0.0)from your java code, will make your button fully transparent.

Or you can also use it in your layout xml. Inside your <Button /> tag use the following attribute:

android:alpha="0"


回答2:

If you use

?android:attr/selectableItemBackground

you'll have a transparent background with Ripple Effect according to this article of android-developers

Here an example

<Button
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Change Password"
    android:background="?android:attr/selectableItemBackground"
    android:textSize="12sp"
    android:gravity="left|center"/>


回答3:

You can use android:alpha="0.5" to set your button half-transparent (the value has to be a float, where 0.0 is invisible and 1.0 is opaque), but be aware that this only works from API Level 11 (Android 3.0) onwards.

Another option would be to make change the background image itself and make it transparent using a graphics program like Gimp or Fireworks.



回答4:

simply if you want to make button transparent you can use

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

or use #0000 four zero in has code.let it only be 4 otherwise it will be considered as black.

hope this helps



回答5:

I know the question is about Button, but it's easy to achieve 'button' that is:

  • Transparent
  • Without shadows or borders
  • With animations

using TextView with attributes:

        android:background="?attr/selectableItemBackground"
        android:clickable="true"

it looks like this:

Code example:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:onClick="onClickMethod"
        android:textStyle="bold"
        android:text="@string/transparent"/>


回答6:

Instead of using a

Button or Image Button

You can use an

Image

and set the attribute

android:onClick="action"