android自定义对话框

来源:互联网 发布:php 清空cookie 编辑:程序博客网 时间:2024/04/30 11:39

 final Dialog dialog=new Dialog(HomeActivity.this,R.style.dialog_bg_style); //在第二个参数中自定对话框的style.

 在资源文件styles.xml中添加一个自定义的style:

<style name="dialog_bg_style" parent="@android:style/Theme.Dialog">//继承对话框的style
        <item name="android:windowFrame">@null</item> //设置前景

<item name="android:background">@drawable/button_pressed</item>//设置背景
        <item name="android:windowIsFloating">true</item>//是否是悬浮窗类型
        <item name="android:windowIsTranslucent">true</item>//设置是否透明
        <item name="android:windowNoTitle">true</item>//去除对话框的标题

        <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>//设置窗口出现的方式
    </style>

接着定义自己的对话框(自己想怎么定义)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#FFFFFF">
    <RelativeLayout android:id="@+id/dialog_one_tv_rl"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/wallpaper">
<TextView android:id="@+id/dialog_one_tip_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="20sp"
    android:gravity="center"
    android:text="新建歌单"
    android:background="@drawable/wallpaper"
    android:layout_marginLeft="@dimen/margin_left"/>
    </RelativeLayout>
   <RelativeLayout android:id="@+id/dialog_one_listname_rl"
       android:layout_width="match_parent"
       android:layout_height="80dp"
       android:layout_below="@+id/dialog_one_tv_rl">
       <EditText 
           android:id="@+id/dialog_one_listname_et"
           android:layout_width="match_parent"
           android:layout_height="45dp"
           android:layout_marginLeft="@dimen/margin_left"
           android:layout_marginRight="@dimen/margin_left"
           android:layout_centerHorizontal="true"
           android:layout_centerVertical="true"
           android:background="@drawable/textview_bg"/>
   </RelativeLayout>
   <LinearLayout android:id="@+id/dialog_one_bt_ll"
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="60dp"
       android:gravity="center_vertical"
       android:layout_below="@+id/dialog_one_listname_rl"
       android:background="@drawable/wallpaper">
       <Button android:id="@+id/dialog_one_positive_bt"
           android:layout_width="wrap_content"
           android:layout_height="40dp"
           android:layout_weight="1"
           android:text="确定"
           android:background="@drawable/button_bg"
           android:layout_marginLeft="@dimen/margin_left"
           android:layout_marginRight="@dimen/margin_left"
           />
       <Button android:id="@+id/dialog_one_nagtive_bt"
           android:layout_width="wrap_content"
           android:layout_height="40dp"
            android:layout_weight="1"
           android:text="取消"
           android:background="@drawable/button_bg"
           android:layout_marginLeft="@dimen/margin_left"
           android:layout_marginRight="@dimen/margin_left"
           />
   </LinearLayout>
</RelativeLayout>

最后 dialog.setContentView(view);完成 结果:




0 0
原创粉丝点击