Android应用的全透明效果--Activity及Dialog的全透明

编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识、前端、后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过!

1.Activity全透明

同学给了这个有趣的代码,现在公布出来。

先在res/values下建colors.xml文件,写入:

1
2
3
4
<?xmlversion= "1.0" encoding= "UTF-8" ?>
<resources>
<colorname= "transparent" > #9000</color>
</resources>

这个值设定了整个界面的透明度,为了看得见效果,现在设为透明度为56%(9/16)左右。

再在res/values/下建styles.xml,设置程序的风格

1
2
3
4
5
6
7
8
<?xml version= "1.0" encoding= "utf-8" ?>
<resources>
     <style name= "Transparent" >
         <item name= "android:windowBackground" >@color/transparent</item>
         <item name= "android:windowIsTranslucent" > true </item> 
         <item name= "android:windowAnimationStyle" >@+android:style/Animation.Translucent</item>
     </style>
</resources>

最后一步,把这个styles.xml用在相应的Activity上。即在AndroidManifest.xml中的任意<activity>标签中添加

1
android:theme = "@style/transparent"

如果想设置所有的activity都使用这个风格,可以把这句标签语句添加在<application>中。

最后运行程序,是不是发现整个界面都被蒙上一层半透明了。最后可以把背景色#9000换成#0000,运行程序后,就全透明了,看得见背景下的所有东西可以却都操作无效。很有趣吧。。。

2.Dialog全透明

1.准备保留边框的全透明素材如下图:

transparent

2.在values中新建一styles.xml文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version= "1.0" encoding= "UTF-8" ?>
<resources>
     <style name= "TANCStyle" parent= "@android:style/Theme.Dialog" >
         <!-- 更换背景图片实现全透明 -->
         <item name= "android:windowBackground" >@drawable/panel_background_sodino1</item>
         <!-- 屏幕背景不变暗 -->
         <item name= "android:backgroundDimEnabled" > false </item>
         <!-- 更改对话框标题栏 -->
         <item name= "android:windowTitleStyle" >@style/TitleStyle</item>
     </style>
     <style name= "TitleStyle" parent= "@android:style/DialogWindowTitle" >
         <item name= "android:textAppearance" >@style/TitleText</item>
     </style>
     <style name= "TitleText" parent= "@android:style/TextAppearance.DialogWindowTitle" >
         <!-- 设置Dialog标题栏文字颜色。 -->
         <item name= "android:textColor" > #000</item>
     </style>
</resources>

3.在layout文件夹下新建一文件句为main_dialog.xml,内容如下:

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
<?xml version= "1.0" encoding= "UTF-8" ?>
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:background= "#0000" >
     <ScrollView android:id= "@+id/ScrollView01"
         android:layout_width= "wrap_content"
         android:layout_height= "200px"
         android:layout_below= "@+id/ImageView01"
         android:background= "#0000" >
         <TextView android:id= "@+id/TextView01"
             android:text= "SodinoText"
             android:textColor= "#f000"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:background= "#0000"
         ></TextView>
     </ScrollView>
     <Button android:id= "@+id/btnCancel"
         android:layout_below= "@id/ScrollView01"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:layout_centerHorizontal= "true"
         android:text= "Cancel" >
     </Button>
</RelativeLayout>

4.Activity代码如下:

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
28
29
30
31
32
33
34
35
36
37
38
39
package lab.sodino.tanc;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TANCAct extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         Button btnShow = (Button) findViewById(R.id.btnShow);
         btnShow.setOnClickListener( new Button.OnClickListener() {
             public void onClick(View view) {
                 showTANC(
                         "This is my custom dialog box" ,
                         "TextContent/nWhen a dialog is requested for the first time, Android calls onCreateDialog(int)  from your Activity, which is where you should instantiate the Dialog. This callback method is passed the same ID that you passed to showDialog(int). After you create the Dialog, return the object at the end of the method." ,
                         "http://blog.csdn.net/sodino" );
             }
         });
     }
     private void showTANC(String header, String content, String url) {
         final Dialog dialog = new Dialog( this , R.style.TANCStyle);
         dialog.setContentView(R.layout.main_dialog);
         dialog.setTitle(header);
         dialog.setCancelable( true );
         TextView textView01 = (TextView) dialog.findViewById(R.id.TextView01);
         textView01.setText(content + content + content);
         Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
         btnCancel.setOnClickListener( new Button.OnClickListener() {
             public void onClick(View view) {
                 dialog.cancel();
             }
         });
         dialog.show();
     }
}

最后效果图:


transparent


//----------------------------------------------------

注意属性:Dialog背景android:backgroundDimEnabled不变暗。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值