Android透明效果的实现继承自View的类都有一个android:background XML属性,按照文档的说法,这个属性不只指
Android透明效果的实现
继承自View的类都有一个android:background XML属性,按照文档的说法,这个属性不只指定背景颜色,还可指定背景图片。背景图片好说,直接用”@drawable/img”指定一幅图片即可,而且支持透明PNG,这样就很足够了。对于单纯颜色,可以使用#rgb”, “#argb”, “#rrggbb”, 或者 “#aarrggbb”等样式的数值,其中的a即alpha、透明度,比如说#ff000000表示不透明的黑色。指定控件的android:background属性为”@android:color/transparent”可实现背景的透明。
再者,有些控件的相关方法可以解决,像是ImageView的setAlpha()什么的.
例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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>
其中的按钮资源是一些透明背景的PNG-8图片
实现效果如下: