我如何使一个Textview的背景大约20%透明(不完全透明),在哪里有一个颜色的背景(即白色)?
当前回答
试试这段代码:)
它是一个完全透明的十六进制代码-“#00000000”
其他回答
我已经接受了三个观点。在第一个视图中,我设置了全颜色(无alpha),在第二个视图中,我设置了半颜色(0.5 alpha),在第三个视图中,我设置了浅色(0.2 alpha)。
你可以使用下面的代码设置任何颜色并使用alpha获取颜色:
文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical"
tools:context = "com.example.temp.MainActivity" >
<View
android:id = "@+id/fullColorView"
android:layout_width = "100dip"
android:layout_height = "100dip" />
<View
android:id = "@+id/halfalphaColorView"
android:layout_width = "100dip"
android:layout_height = "100dip"
android:layout_marginTop = "20dip" />
<View
android:id = "@+id/alphaColorView"
android:layout_width = "100dip"
android:layout_height = "100dip"
android:layout_marginTop = "20dip" />
</LinearLayout>
文件MainActivity.java
public class MainActivity extends Activity {
private View fullColorView, halfalphaColorView, alphaColorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fullColorView = (View)findViewById(R.id.fullColorView);
halfalphaColorView = (View)findViewById(R.id.halfalphaColorView);
alphaColorView = (View)findViewById(R.id.alphaColorView);
fullColorView.setBackgroundColor(Color.BLUE);
halfalphaColorView.setBackgroundColor(getColorWithAlpha(Color.BLUE, 0.5f));
alphaColorView.setBackgroundColor(getColorWithAlpha(Color.BLUE, 0.2f));
}
private int getColorWithAlpha(int color, float ratio) {
int newColor = 0;
int alpha = Math.round(Color.alpha(color) * ratio);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
newColor = Color.argb(alpha, r, g, b);
return newColor;
}
}
芬兰湾的科特林版:
private fun getColorWithAlpha(color: Int, ratio: Float): Int {
return Color.argb(Math.round(Color.alpha(color) * ratio), Color.red(color), Color.green(color), Color.blue(color))
}
Done
我建议使用alpha属性。
<TextView
android:alpha="0.8" />
或者现在你可以使用选择器。在colors包中创建background_color_25.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.8" android:color="@color/background_color" />
</selector>
用法如下:
<TextView
android:background="@color/background_color_25" />
在Kotlin中,你可以像这样使用alpha,
//Click on On.//
view.rel_on.setOnClickListener{
view.rel_off.alpha= 0.2F
view.rel_on.alpha= 1F
}
//Click on Off.//
view.rel_off.setOnClickListener {
view.rel_on.alpha= 0.2F
view.rel_off.alpha= 1F
}
结果如屏幕截图所示。
希望这对你有所帮助。谢谢
试试这段代码:)
它是一个完全透明的十六进制代码-“#00000000”
使颜色在alpha通道中占80%。例如,对于红色,使用#CCFF0000:
<TextView
...
android:background="#CCFF0000" />
在本例中,CC为255 * 0.8 = 204的十六进制数。注意,前两个十六进制数字是用于alpha通道的。格式为#AARRGGBB,其中AA为alpha通道,RR为红色通道,GG为绿色通道,BB为蓝色通道。
我假设20%透明意味着80%不透明。如果你的意思是另一种方式,用33代替CC,这是255 * 0.2 = 51的十六进制。
为了计算alpha透明度值的正确值,您可以遵循以下步骤:
Given a transparency percentage, for example 20%, you know the opaque percentage value is 80% (this is 100-20=80) The range for the alpha channel is 8 bits (2^8=256), meaning the range goes from 0 to 255. Project the opaque percentage into the alpha range, that is, multiply the range (255) by the percentage. In this example 255 * 0.8 = 204. Round to the nearest integer if needed. Convert the value obtained in 3., which is in base 10, to hexadecimal (base 16). You can use Google for this or any calculator. Using Google, type "204 to hexa" and it will give you the hexadecimal value. In this case it is 0xCC. Prepend the value obtained in 4. to the desired color. For example, for red, which is FF0000, you will have CCFF0000.
你可以看看Android的颜色文档。
推荐文章
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?