您的位置:首页 > 移动开发 > Android开发

Androidの自定义对话框AlertDialog(二)

2015-09-10 15:12 826 查看

Android自定义View----3.对话框Dialog

本节前言

在Android中,对话框用于一些重要的提示信息,或者对用户额外交互的一些内容很有帮 助。本篇讲解一下Android下对话框的使用,在本篇博客中,将了解到对话框的一些常规属性的设置,以及各式的对话框的使用,并都会提供小 Demo来展示所有的效果。

Dialog

  Dialog,对话框,一个对话框就是一个小窗口,并不会填满整个屏幕,通常是以模态显示,要求用户必须采取行动才能继

本节讲解自定义对话框Dialog使用.

本节正文


1. 简介

自定义对话框Dialog样式非常多,看起来就是一个简单弹出框提示,可以替换掉传统的toast, 黑色背景,实现也很容易,这里权当做个笔记,方便后续使用。截图如下所示:



java 源码

private void showMessage(String msg) {
Dialog  dialog =new Dialog(AddLeaveActivity.this, R.style.dialog); // 定义一个Dialog
View view =LayoutInflater.from(AddLeaveActivity.this).inflate(R.layout.dialog_layout,null);
TextView textview=(TextView)view.findViewById(R.id.dialog_textview);
textview.setText(msg);
dialog.setContentView(view);
dialog.show();
}
完全是定义一个布局xml,, 加载入Dialog 对象中
xml源码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dip"
android:layout_height="250dip"
android:background="@drawable/title_bg">

<TextView
android:id="@+id/dialog_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="14.0sp"
android:textColor="@color/white"/>
</RelativeLayout>

注意:如果想更改对话框中样式,可以更改xml 文件,添加额外的组件,或者更改背景等,针对该xml做修改即可实现.

但是android自定义对话框时候,往往会出现黑边情况,此时处理方式是加上一个style , 使其背景色透明
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.6</item>
</style>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: