android自定义Dialog弹出框样式(真正有用的)

网上许多关于自定义Dialog的文章,但是真正有用的没几个,很多都是骗人的,为了让广大码农少走弯路,特献上此文


android自定义Dialog弹出框样式(真正有用的)_第1张图片

一、先看styles.xml里自定义的背景样式:

<style name="MyDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@nullitem>
    <item name="android:windowNoTitle">trueitem>
    <item name="android:windowBackground">@android:color/transparentitem>
    <item name="android:windowIsFloating">trueitem>
    <item name="android:windowContentOverlay">@nullitem>
style>

其中windowBackground 的颜色transparent是透明的,其在colors.xml中的代码是

 
  
<color name="transparent">#00000000color>

 
  
二、自定义Dialog布局 custom_dialog.xml

 
  
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20.0dip"
    android:background="@drawable/p_dialog_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20.0dip">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10.0dip">

        <TextView
            android:id="@id/dialog_tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="10.0dip"
            android:textColor="@color/white"
            android:textSize="20.0sp" />
    RelativeLayout>

    <TextView
        android:id="@id/dialog_tv_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingLeft="15.0dip"
        android:textColor="@color/white"
        android:textSize="18.0sp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10.0dip">

        <Button
            android:id="@id/dialog_button_cancel"
            android:layout_width="fill_parent"
            android:layout_height="45.0dip"
            android:layout_weight="1.0"
            android:background="@drawable/p_at_dialog_btn_left_selector"
            android:text="取    消"
            android:textColor="@color/white" />

        <View
            android:id="@id/dialog_view"
            android:layout_width="20.0dip"
            android:layout_height="fill_parent" />

        <Button
            android:id="@id/dialog_button_ok"
            android:layout_width="fill_parent"
            android:layout_height="45.0dip"
            android:layout_weight="1.0"
            android:background="@drawable/p_at_dialog_btn_right_selector"
            android:text="确    定"
            android:textColor="@color/white" />
    LinearLayout>
LinearLayout>


布局用到的背景文件如下:
 
  
p_dialog_bg.png
 
  
布局用到的按钮样式如下:
“取消”按钮 p_at_dialog_btn_left_selector.xml
 
  
xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/p_dialog_btn_left_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/p_dialog_btn_left_pressed" />
    <item android:drawable="@drawable/p_dialog_btn_left_unpressed" />
selector>

该按钮用到的图片如下:
 
  
p_dialog_btn_left_pressed.png
 
  
p_dialog_btn_left_unpressed.png

"确定"按钮 p_at_dialog_btn_right_selector.xml
 
  
xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/p_dialog_btn_right_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/p_dialog_btn_right_pressed" />
    <item android:drawable="@drawable/p_dialog_btn_right_unpressed" />
selector>

该按钮用到的图片如下:
 
  
p_dialog_btn_right_pressed.png
p_dialog_btn_right_unpressed.png

 
  
 
  
 
  
三、Activity中调用:

 
  
final Dialog dialog = new Dialog(this, R.style.MyDialog);
//设置它的ContentView
dialog.setContentView(R.layout.custom_dialog);
((TextView)dialog.findViewById(R.id.dialog_tv_title)).setText("提示");
((TextView)dialog.findViewById(R.id.dialog_tv_content)).setText("是否清理缓存?");
dialog.show();

Button confirm = (Button)dialog.findViewById(R.id.dialog_button_ok);
confirm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
        DataUtils.showToast(context,"数据已删除");
    }
});
Button cancel = (Button) dialog.findViewById(R.id.dialog_button_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        dialog.dismiss();
    }
});

 
  

你可能感兴趣的:(android开发)