让一个View透明

本文介绍了在Android中如何为视图组件设置背景颜色及背景图片。不仅涵盖了如何使用颜色代码定义背景,还展示了如何通过XML布局文件指定背景资源,包括支持透明度调整的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继承自View的类都有一个android:background XML属性,按照文档的说法,这个属性不只指定背景颜色,还可指定背景图片。背景图片好说,直接用"@drawable/img"指定一幅图片即可,而且支持透明PNG,这样就很足够了。对于单纯颜色,可以使用#rgb", "#argb", "#rrggbb", 或者 "#aarrggbb"等样式的数值,其中的a即alpha、透明度,比如说#ff000000表示不透明的黑色。指定控件的android:background属性为"@android:color/transparent"可实现背景的透明。

再者,有些控件的相关方法可以解决,像是ImageView的setAlpha()什么的.

例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/wallpaper"
    >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#86222222"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
 <Button
 android:background="@drawable/chat"
 android:id="@+id/bt_chat"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
     />
 <Button
 android:background="@drawable/calendar"
 android:id="@+id/bt_calendar"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
     />
</LinearLayout>
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值