Android对话框-下篇-之设置activity为Dialog

本文指导如何在Android中创建一个半透明的悬浮窗口应用,包括主题设置、XML布局和关键代码实现。

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


 

 

 

[html]  view plain copy
  1. package com.mrzhu.dialogtest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7.   
  8. public class DialogTestActivity extends Activity {  
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.     }  
  15.       
  16.     public void click(View v){  
  17.         startActivity(new Intent(DialogTestActivity.this, Activity2.class));  
  18.     }  
  19. }  


[html]  view plain copy
  1. package com.mrzhu.dialogtest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Window;  
  6. import android.view.WindowManager;  
  7. import android.view.WindowManager.LayoutParams;  
  8.   
  9. public class Activity2 extends Activity {  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity2);  
  14.         Window window = getWindow();  
  15.         WindowManager.LayoutParams layoutParams = window.getAttributes();  
  16.         //设置窗口的大小及透明度  
  17.         layoutParams.width = LayoutParams.FILL_PARENT;  
  18.         layoutParams.height = layoutParams.FILL_PARENT;  
  19.         layoutParams.alpha = 0.5f;  
  20.         window.setAttributes(layoutParams);  
  21.     }  
  22. }  


AndroidManifest.xml中加入:

[html]  view plain copy
  1. <activity  
  2.            android:name=".Activity2"  
  3.            android:theme="@style/Transparent" >  
  4.        </activity>  


style.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <style name="Transparent" parent="android:style/Theme.Dialog">  
  5.         <item name="android:windowNoTitle">true</item>  
  6.         <item name="android:windowIsTranslucent">true</item>  
  7.         <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>  
  8.     </style>  
  9.   
  10. </resources>  


main.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:onClick="click"  
  11.         android:text="click" />  
  12.   
  13. </LinearLayout>  
有人希望做出来的应用程序是一个漂浮在手机主界面的东西,那么很
简单你只需要设置一下Activity的主题就可以了在AndroidManifest.xml 中定义Activity的
地方一句话:
 
android:theme="@android:style/Theme.Dialog"
 
这就使你的应用程序变成对话框的形式弹出来了,或者
 
android:theme="@android:style/Theme.Translucent"
就变成半透明的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值