Android编程实现对话框Dialog背景透明功能示例

所属分类: 软件编程 / Android 阅读数: 68
收藏 0 赞 0 分享

本文实例讲述了Android编程实现对话框Dialog背景透明功能。分享给大家供大家参考,具体如下:

先看效果:

 

这是我做的一个拨号器强的面板,拨号的时候会查询手机中的联系人,显示在拨号面板上方,点击弹出透明对话框供选择。

这次重点是透明对话框。

先看对话框的theme,style文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style
    name="selectorDialog"
    parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item><!--边框-->
    <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
    <item name="android:windowIsTranslucent">false</item><!--半透明-->
    <item name="android:windowNoTitle">true</item><!--无标题-->
    <item name="android:windowBackground">@drawable/selector_dialog_bg</item><!--背景透明-->
    <item name="android:backgroundDimEnabled">false</item><!--模糊-->
    <item name="android:backgroundDimAmount">0.6</item>
  </style>
</resources>

对话框背景@drawable/selector_dialog_bg:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <solid android:color="#333333"/>
  <stroke
    android:width="2dp"
    android:color="#99CC33" />
  <padding
    android:left="5dp"
    android:top="5dp"
    android:right="5dp"
    android:bottom="5dp" />
  <corners android:radius="8dp" />
</shape>

然后是对话框的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" >
  <ListView
    android:id="@+id/selector_dialog_listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:scrollbars="none"
    android:dividerHeight="1.0dip"
    android:divider="#C4C4C4" />
</LinearLayout>

程序中:

final View view = LayoutInflater.from(this).inflate(R.layout.selector_dialog, null);
selectorDialog = new Dialog(DialerActivity.this, R.style.selectorDialog);
selectorDialog.setContentView(view);
final BaseAdapter adapter = new SelectorAdapter(DialerActivity.this, selectorList);
ListView listView = (ListView) view.findViewById(R.id.selector_dialog_listview);
listView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //对话框中事件处理
  }
});
listView.setAdapter(adapter);
selectorDialog.show();
selectorDialog.setCanceledOnTouchOutside(true);
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = selectorDialog.getWindow().getAttributes();
lp.width = (int)(display.getWidth() * 0.9);
if(selectorList.size() > 7) {
  lp.height = (int)(display.getHeight() * 0.9);
}
lp.alpha = 0.8f;
selectorDialog.getWindow().setAttributes(lp);

其实主要是通过WindowManager.LayoutParams给对话框设置属性。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多