Android编程如何实现对话框Dialog背景透明功能

发布时间:2021-07-10 10:16:38 作者:小新
来源:亿速云 阅读:276

这篇文章主要介绍Android编程如何实现对话框Dialog背景透明功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体如下:

先看效果:

Android编程如何实现对话框Dialog背景透明功能 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编程如何实现对话框Dialog背景透明功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 自定义Dialog对话框
  2. 向Dialog对话框嵌入Listview

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android dialog

上一篇:正则表达式如何验证IPV4地址功能

下一篇:php如何实现表单正则验证及文件上传验证功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》