使用自定义透明Dialog样式的Activity

有时你需要一个对话框,但同时对话框中的内容有更多控制和能控制其生命周期,这时你可以使用带有Dialog样式的Activity来应用你的项目中,

想使Activity有对话框那样效果可以在Androidmanifest中添加 Android:style/Theme.Dialog 的主题特性 

例如这样:

[html]  view plain  copy
 print ?
  1. <activity android:name=”MyDialogActivity”  
  2.        android:theme=”@android:style/Theme.Dialog”>  
  3. </activity>  


如果你觉得上面不足以满足你的需求想实现 半透明的, 圆角的,不妨可以试试自定义对话框的style

首先

第一步: 准备工作要做好 一张.9图片 

    loading_bg.9.png

第二步 :自定义style   在你项目 values 文件夹中创建一个styles.xml 文件

 接着在styles文件中添加 名为 MyDialogStyle的style

[html]  view plain  copy
 print ?
  1. <style name="MyDialogStyle">  
  2.         <item name="android:windowBackground">@android:color/transparent</item>  
  3.         <item name="android:windowFrame">@null</item>  
  4.         <item name="android:windowNoTitle">true</item>  
  5.         <item name="android:windowIsFloating">true</item>  
  6.         <item name="android:windowIsTranslucent">true</item>  
  7.         <item name="android:windowContentOverlay">@null</item>  
  8.         <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>  
  9.         <item name="android:backgroundDimEnabled">true</item>  
  10. </style>  

第三步:Dialog的布局 在layout文件夹中添加布局文件  activity_main.xml

[html]  view plain  copy
 print ?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">    
  5.   
  6.     <RelativeLayout  
  7.         android:layout_width="180dp"  
  8.         android:layout_height="180dp"  
  9.         android:layout_centerInParent="true"  
  10.         android:background="@drawable/loading_bg" >  
  11.   
  12.         <LinearLayout  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="fill_parent"  
  15.             android:layout_alignParentLeft="true"  
  16.             android:layout_alignParentTop="true"  
  17.             android:gravity="center"  
  18.             android:orientation="vertical" >  
  19.   
  20.             <ProgressBar  
  21.                 android:id="@+id/progressBar1"  
  22.                 style="?android:attr/progressBarStyleLarge"  
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:layout_gravity="center_horizontal" />  
  26.   
  27.             <TextView  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:layout_marginTop="10dp"  
  31.                 android:text="正在登录"  
  32.                 android:textColor="#fff"  
  33.                 android:textSize="20sp" />  
  34.         </LinearLayout>  
  35.   
  36.     </RelativeLayout>  
  37.   
  38. </RelativeLayout>  

创建 DialogActivity类 

[java]  view plain  copy
 print ?
  1. public class DialogActivity extends Activity  
  2. {  
  3.   
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         // 这里你可以进行一些等待时的操作,我这里用8秒后显示Toast代理等待操作  
  10.         new Handler().postDelayed(new Runnable(){  
  11.             @Override  
  12.             public void run(){  
  13.                   
  14.                 DialogActivity.this.finish();  
  15.                 Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();  
  16.             }  
  17.         }, 8000);  
  18.     }  
  19. }  

第四步 : 在Manifest文件中对DialogActivity进行注册,并使用上面自定义MyDialogStyle样式

[html]  view plain  copy
 print ?
  1. <activity  
  2.      android:name="LoadingActivity"  
  3.      android:theme="@style/MyDialogStyle" />  
  4. <activity  

第五步:运行工程并查看效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值