简体   繁体   English

您可以在Android中使用透明背景的按钮自定义图片吗?

[英]Can you have a custom image for a button with a transparent background in android?

I'm making custom buttons for an android application, but I want the buttons to look like an overlay over a video being played. 我正在为android应用程序制作自定义按钮,但我希​​望这些按钮看起来像是正在播放的视频的叠加层。

At the moment the buttons get displayed over the video, but instead of having a transparent background the background is white. 目前,按钮显示在视频上,但背景不是白色,而是透明的。 It looks like this: 看起来像这样:

Buttons over video 视频按钮

I'm setting the background for the button with an xml file in the drawable folder, so I can set different images for the different states. 我在drawable文件夹中使用xml文件设置按钮的背景,因此可以为不同的状态设置不同的图像。 Therefore I can't just set android:background="color/transparent" because my line is android:background="@drawable/custom_button". 因此,我不能只设置android:background =“ color / transparent”,因为我的行是android:background =“ @ drawable / custom_button”。 Where I am setting the images like this: item android:drawable="@drawable/left_default_small". 我在哪里设置像这样的图像:项目android:drawable =“ @ drawable / left_default_small”。

Is there a line that can fix this or do I have to change a styles file or something? 有没有可以解决此问题的行,或者我必须更改样式文件或其他内容吗?

btw I'm using API 21 so that's why the shadow is default, but I don't know how to fix it other than changing the API 顺便说一句,我正在使用API​​ 21,所以这就是默认阴影的原因,但是除了更改API外,我不知道如何解决它

Try to use ImageButton 尝试使用ImageButton

<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@null"></ImageButton>

OR 要么

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

If you want to create a StateListDrawable like this one in res/drawable directory. 如果要在res / drawable目录中创建像这样的StateListDrawable。

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

This way the default background is transparent. 这样,默认背景是透明的。 When pressed the background has the color you specified (instead of color you can use any drawable here). 按下时,背景具有您指定的颜色(您可以在此处使用任何可绘制对象来代替颜色)。

use this for global backgroud store this in your res/drawable and call this drawable for transperent image 将其用于全局背景将其存储在res / drawable中,并将其称为transable图像

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="100dp"
        />
    <gradient
        android:angle="45"
        android:centerX="35%"
        android:centerColor="#00000000"
        android:startColor="#00000000"
        android:endColor="#00000000"
        android:type="linear"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <size
        android:width="100dp"
        android:height="100dp"
        />
    <stroke
        android:width="1dp"
        android:color="#99000000"
        />
</shape>

or simply use 或简单地使用

android:background="#00000000"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM