Android中使用Dialog风格弹出框的Activity

来源:互联网 发布:mysql 高并发解决方案 编辑:程序博客网 时间:2024/05/21 09:46

在Android中经常会遇到需要使用Dialog风格弹出框的activity,首先我们可能会首先想到的是在XML布局文件中设置android:layout_height="wrap_content"属性,让activity的高度自适应,显然这还不行,我们还需要为其DialogActivity设置自定义一个样式

[html] view plain copy
  1. <style name="dialogstyle">  
  2.         <!--设置dialog的背景-->  
  3.         <item name="android:windowBackground">@android:color/transparent</item>  
  4.         <!--设置Dialog的windowFrame框为无-->  
  5.         <item name="android:windowFrame">@null</item>  
  6.         <!--设置无标题-->  
  7.         <item name="android:windowNoTitle">true</item>  
  8.         <!--是否浮现在activity之上-->  
  9.         <item name="android:windowIsFloating">true</item>  
  10.         <!--是否半透明-->  
  11.         <item name="android:windowIsTranslucent">true</item>  
  12.         <!--设置窗口内容不覆盖-->  
  13.         <item name="android:windowContentOverlay">@null</item>  
  14.         <!--设置动画,在这里使用让它继承系统的Animation.Dialog-->  
  15.         <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>  
  16.         <!--背景是否模糊显示-->  
  17.         <item name="android:backgroundDimEnabled">true</item>  
  18.     </style>  
然后在AndroidManifest.xml中设置DialogActivity的样式为我们自定义的dialogstyle


如下是布局的代码

[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="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@color/white"  
  6.     android:orientation="vertical">  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="65dp"  
  11.         android:orientation="horizontal"  
  12.         android:paddingLeft="@dimen/acitvity_margin"  
  13.         android:paddingRight="@dimen/acitvity_margin">  
  14.   
  15.         <LinearLayout  
  16.             android:layout_width="0dp"  
  17.             android:layout_height="match_parent"  
  18.             android:layout_weight="1"  
  19.             android:orientation="horizontal">  
  20.   
  21.             <TextView  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="match_parent"  
  24.                 android:gravity="center"  
  25.                 android:text="上班时间:"  
  26.                 android:textColor="@color/grey"  
  27.                 android:textSize="@dimen/size_text_medium" />  
  28.   
  29.             <Button  
  30.                 android:id="@+id/tv_signin_time"  
  31.                 android:layout_width="wrap_content"  
  32.                 android:layout_height="match_parent"  
  33.                 android:background="@color/white"  
  34.                 android:gravity="center"  
  35.                 android:text="9:00"  
  36.                 android:textColor="@color/grey"  
  37.                 android:textSize="@dimen/size_text_medium" />  
  38.         </LinearLayout>  
  39.   
  40.         <LinearLayout  
  41.             android:layout_width="0dp"  
  42.             android:layout_height="match_parent"  
  43.             android:layout_weight="1"  
  44.             android:orientation="horizontal">  
  45.   
  46.             <TextView  
  47.                 android:layout_width="wrap_content"  
  48.                 android:layout_height="match_parent"  
  49.                 android:gravity="center"  
  50.                 android:text="下班时间:"  
  51.                 android:textColor="@color/grey"  
  52.                 android:textSize="@dimen/size_text_medium" />  
  53.   
  54.             <Button  
  55.                 android:id="@+id/tv_signout_time"  
  56.                 android:layout_width="wrap_content"  
  57.                 android:layout_height="match_parent"  
  58.                 android:background="@color/white"  
  59.                 android:gravity="center"  
  60.                 android:text="18:00"  
  61.                 android:textColor="@color/grey"  
  62.                 android:textSize="@dimen/size_text_medium" />  
  63.         </LinearLayout>  
  64.     </LinearLayout>  
  65.   
  66.     <RelativeLayout  
  67.         android:layout_width="match_parent"  
  68.         android:layout_height="65dp"  
  69.         android:paddingLeft="@dimen/acitvity_margin"  
  70.         android:paddingRight="@dimen/acitvity_margin">  
  71.   
  72.         <TextView  
  73.             android:layout_width="wrap_content"  
  74.             android:layout_height="match_parent"  
  75.             android:layout_alignParentLeft="true"  
  76.             android:gravity="center"  
  77.             android:text="公司位置:"  
  78.             android:textColor="@color/grey"  
  79.             android:textSize="@dimen/size_text_medium" />  
  80.   
  81.         <EditText  
  82.             android:id="@+id/et_address"  
  83.             android:layout_width="wrap_content"  
  84.             android:layout_height="match_parent"  
  85.             android:layout_marginLeft="2dp"  
  86.             android:layout_toRightOf="@+id/tv_address"  
  87.             android:background="@color/white"  
  88.             android:hint="请输入公司位置"  
  89.             android:singleLine="true"  
  90.             android:textSize="@dimen/size_text_small" />  
  91.   
  92.         <TextView  
  93.             android:id="@+id/tv_location"  
  94.             android:layout_width="wrap_content"  
  95.             android:layout_height="wrap_content"  
  96.             android:layout_alignParentRight="true"  
  97.             android:layout_centerInParent="true"  
  98.             android:gravity="center"  
  99.             android:padding="5dp"  
  100.             android:text="重新定位"  
  101.             android:textColor="@color/blue"  
  102.             android:textSize="@dimen/size_text_medium" />  
  103.     </RelativeLayout>  
  104.   
  105.     <RelativeLayout  
  106.         android:layout_width="match_parent"  
  107.         android:layout_height="65dp"  
  108.         android:paddingLeft="@dimen/acitvity_margin"  
  109.         android:paddingRight="@dimen/acitvity_margin">  
  110.   
  111.         <TextView  
  112.             android:layout_width="wrap_content"  
  113.             android:layout_height="match_parent"  
  114.             android:layout_alignParentLeft="true"  
  115.             android:gravity="center"  
  116.             android:text="设置管理员:"  
  117.             android:textColor="@color/grey"  
  118.             android:textSize="@dimen/size_text_medium" />  
  119.   
  120.         <ImageView  
  121.             android:layout_width="wrap_content"  
  122.             android:layout_height="match_parent"  
  123.             android:layout_alignParentRight="true"  
  124.             android:gravity="center"  
  125.             android:src="@mipmap/icon_toright" />  
  126.     </RelativeLayout>  
  127. </LinearLayout>  

接下来我们再看一下效果图是不是我们想要的呢


阅读全文
0 0
原创粉丝点击