[英]How to apply transparency only on background color of buttons
I'm a beginner on Android Studio and I need your help to apply a specific transparency on my layout. 我是Android Studio的初学者,需要您的帮助才能在版式上应用特定的透明度。
I applied a background and border color to my layout by using this drawable: 我通过使用此drawable将背景和边框颜色应用于布局:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/black" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#ffa500" />
</shape>
Now I want to apply dynamically a transparency to this button ( by using a seek bar ) but only on the background color I don't want to modify my border color. 现在,我想动态地对此按钮应用透明性( 通过使用搜索栏 ),但仅在背景颜色上,我不想修改边框颜色。 I tried to use setAlpha function but it also change my border color transparency.
我尝试使用setAlpha函数,但它也更改了边框颜色的透明度。
How can I do this? 我怎样才能做到这一点? Thanks.
谢谢。
You need to use the alpha portion of the color your specify in item. 您需要使用您在项目中指定的颜色的Alpha部分。
For example - #aarrggbb -- aa part is the alpha. 例如-#aarrggbb-一个部分是Alpha。
If you want to change stroke / solid color separately, I recommend using GradientDrawable to setStroke()
and `setColor()'. 如果要单独更改笔触/纯色,建议将GradientDrawable
setStroke()
和`setColor()'。
For reference - 以供参考 -
https://developer.android.com/reference/android/graphics/drawable/GradientDrawable#setColor(int) https://developer.android.com/reference/android/graphics/drawable/GradientDrawable#setColor(int)
https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setStroke(int,%20android.content.res.ColorStateList,%20float,%20float) https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setStroke(int,%20android.content.res.ColorStateList,%20float,%20float)
In your shape change the <solid android:color="@android:color/black" />
to <solid android:color="#00FFFFFF" />
This will make your shape transparent. 在您的形状中,将
<solid android:color="@android:color/black" />
更改为<solid android:color="#00FFFFFF" />
这将使您的形状透明。 And in your xml <Button .... android:background="@drawable/your_shape" />
并在您的xml
<Button .... android:background="@drawable/your_shape" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.