简体   繁体   English

如何仅在按钮的背景色上应用透明度

[英]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.

相关问题 如何为VerticalFieldManager和/或Horizo​​ntalFieldManager设置背景透明度和颜色? - How to set background transparency and color to VerticalFieldManager and/or HorizontalFieldManager? 如何更改所有按钮的背景色? - How Do You Change The Background Color of All Buttons? 如何使用 xml 文件设置按钮的背景颜色? - How set background color for buttons using xml files? 如何相互交换两个按钮的背景颜色? - How can I exchange the background color of two buttons with each other? 如何使用Java代码使用HSSF应用单元格背景色 - How to apply cell background color using HSSF using Java code Java合成器 - 如何将纯色应用于按钮背景 - Java synth - How to apply solid color to button background 如何使用 Apache POI 为 Excel 工作表中的行应用背景颜色? - How to apply background color for the rows in excel sheet using Apache POI? 如何在iText7中将背景颜色应用于带有圆角的表格单元格? - How to apply background color to table cell with round corners in iText7? 如何将透明度应用于缓冲图像java - how to apply transparency to a bufferd image java 如何从颜色阵列列表中绘制随机颜色并将其应用于另一个阵列列表中的按钮背景? - How to draw a random color from a color array list and apply it to the backgrounds of buttons from another array list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM