Android中半透明Activity效果另法

Android中的Activity有没有类似于像Windows程序样的窗口式显示呢?

答案当然是有。

下图就是一个窗口式Activity的效果图:

miniActivity

下面就说说实现过程:

首先看看AndroidManifest.xml

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   3:       package="com.hi.braincol.local"
   4:       android:versionCode="1"
   5:       android:versionName="1.0">
   6:     <uses-sdk android:minSdkVersion="8" />
   7:  
   8:     <application android:icon="@drawable/icon" android:label="@string/app_name">
   9:         <activity android:name=".MiniActivity"
  10:                      android:theme="@style/Translucent"
  11:                   android:label="@string/app_name">
  12:             <intent-filter>
  13:                 <action android:name="android.intent.action.MAIN" />
  14:                 <category android:name="android.intent.category.LAUNCHER" />
  15:             </intent-filter>
  16:         </activity>
  17:  
  18:     </application>
  19: </manifest>
第10行:
android:theme="@style/Translucent"
这个就是关键,只要将个这个style设置为窗口式的,那么MiniActivity就会是窗口式的了。
下面的就是这个style的代码:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <style name="Translucent" parent="@android:style/Theme.Translucent">
      <item name="android:windowBackground">@drawable/panel_background</item>
      <item name="android:windowNoTitle">true</item>
      <item name="android:windowIsFloating">true</item>
      <item name="android:windowIsTranslucent">true</item>
      <item name="android:windowContentOverlay">@null</item>
      <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
      <item name="android:backgroundDimEnabled">true</item>
  </style>
</resources>
这样就行了,实现起来还是很简单的聚会笑脸

http://www.cnblogs.com/hibraincol/archive/2011/08/28/2156310.html


在做项目时,常需要用到对话框之类的效果,然而对话框不容易写,所以就想写个半透明的Activity来代替对话框效果,这样的好处至少有三个:

一:布局容易

二:各种控件容易控制

三:代码简练:不至于将控制对话框的代码写在一坨,各种控制也不用写在一坨,易于维护

而现在在网上搜索半透明的Activity时,都是需要在style中写样式,在color中定义颜色,当然,最后还得在Mainfest中配置好,结果一个小小的半透明Activity效果写的好零乱,而且网上来来去去就那几种方法,看得蛋疼,因此为了装逼,我介绍一种简易的半透明Activity效果:

步骤:

一:在Activity的布局的根标签中写入透明颜色:


android:background="#80000000"



二:在清单文件中相应的activity中配置:

android:theme="@android:style/Theme.Translucent.NoTitleBar"

ok,就这两步,

这样的效果好处在于:

一:简单,就两行代码

二:易修改,就两行代码,只需修改一处即可。

效果图:

详细代码:

一:布局:

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#80000000" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentBottom="true"  
  11.         android:layout_marginBottom="40dp"  
  12.         android:layout_marginLeft="18dp"  
  13.         android:layout_marginRight="18dp"  
  14.         android:orientation="vertical" >  
  15.   
  16.         <TextView  
  17.             android:id="@+id/tv_delete"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:background="@drawable/rect_delete_red"  
  21.             android:gravity="center"  
  22.             android:paddingBottom="15dp"  
  23.             android:paddingTop="15dp"  
  24.             android:text="删除"  
  25.             android:textColor="#FFFFFF"  
  26.             android:textSize="20sp" />  
  27.   
  28.         <TextView  
  29.             android:id="@+id/tv_cancel"  
  30.             android:layout_width="match_parent"  
  31.             android:layout_height="wrap_content"  
  32.             android:layout_marginTop="20dp"  
  33.             android:background="@drawable/rect_delete_gray"  
  34.             android:gravity="center"  
  35.             android:paddingBottom="15dp"  
  36.             android:paddingTop="15dp"  
  37.             android:text="取消"  
  38.             android:textColor="#FFFFFF"  
  39.             android:textSize="20sp" />  
  40.     </LinearLayout>  
  41.   
  42. </RelativeLayout>  



二:清单文件:

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. <activity android:name="com.itcode.DialogActivity"  
  2.           android:theme="@android:style/Theme.Translucent.NoTitleBar" ></activity>  



http://blog.csdn.net/sunalongl/article/details/20544177

原文地址:https://www.cnblogs.com/daishuguang/p/3867699.html