让一个View透明

来源:互联网 发布:中国电视网络电视台 编辑:程序博客网 时间:2024/05/01 13:39

继承自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>

原创粉丝点击