Android 自定义对话框

效果图:

1.文字+按钮对话框:

 

(1)编写对话框背景形状(建立已个4个圆角,背景透明的形状),名称为roundrect

<?xml version="1.0"encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
    <item
android:top="1dp"android:bottom="1dp"android:left="1dp"android:right="1dp">
        <shape>
            <solid
android:color="#FFFFFF"/>
            <corners
android:topLeftRadius="10dp"                     android:topRightRadius="10dp"
                    
android:bottomRightRadius="10dp"                  android:bottomLeftRadius="10dp"/>
        </shape>
    </item>
</layer-list>

(2)创建对话框layout,名称为dialog_alarm

 

<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_margin="20dip"
             android:layout_gravity="center"
              android:background="@drawable/roundrect"//引用对话框形状

             android:id="@+id/df">
    <RelativeLayoutandroid:layout_width="@dimen/dialog_width"
                   android:layout_height="match_parent"
                   android:background="@drawable/shape_top_corner"
    >
        <TextViewandroid:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:textColor="@color/white"
                 android:layout_marginLeft="10dip"
                  android:text="@string/str_alert"
                 android:textSize="30dip"
                 android:id="@+id/tvTitle"/>

    </RelativeLayout>
    <RelativeLayoutandroid:layout_width="match_parent"
                   android:layout_height="wrap_content"
                    android:layout_marginTop="10dip">

        <TextViewandroid:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:textColor="@color/gray"
                 android:layout_marginLeft="10dip"
                 android:text="@string/str_alert_message"
                 android:textSize="15dip"
                 android:id="@+id/tvTitl2e"/>

    </RelativeLayout>
    <LinearLayoutandroid:layout_width="match_parent"
                  android:layout_height="wrap_content"
                 android:layout_marginTop="10dip"
                 android:orientation="horizontal"
    >
        <Button
               android:background="@drawable/button_click"
               android:textColor="@color/white"
               android:layout_margin="10dip"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:text="@string/str_certain"
               android:id="@+id/btCertain"
                android:layout_alignParentTop="true"
               android:layout_alignParentLeft="true"
               android:layout_weight="1"
        />
        <Button
               android:background="@drawable/button_click"
                android:textColor="@color/white"
               android:layout_margin="10dip"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:text="@string/str_cancel"
               android:id="@+id/btCancel"
                android:layout_alignParentTop="true"
               android:layout_toRightOf="@+id/btCertain"
               android:layout_weight="1"
                />

    </LinearLayout>

</LinearLayout>

(3)添加自定义对话框类

public class CustomDialog extends Dialog {

    public CustomDialog(Context context,View layout, int style) {
        super(context, style);
        setContentView(layout);
        Window window = getWindow();
        WindowManager.LayoutParams params= window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }
}

(4)在style中添加对话框样式

<style name="DialogStyle"parent="@android:style/Theme.Dialog">
    <itemname="android:windowNoTitle">true</item> <!--对话框无title-->
    <item name="android:windowBackground">@android:color/transparent</item><!--自定义布局以外的部分变透明-->
    <itemname="android:backgroundDimEnabled">true</item> <!--背景是否变暗-->
    <itemname="android:backgroundDimAmount">0.6</item>
</style>

 

(5)在程序中创建自定义对话框

//函数:creatRestoreDialog
//功能:创建恢复出厂设置对话框
private void creatRestoreDialog() {
    LayoutInflater inflater =LayoutInflater.from(SettingsActivity.this);
    View View =inflater.inflate(R.layout.dialog_alarm, null);//引用对话框layout文件
    restoreDialog = newCustomDialog(SettingsActivity.this, View, R.style.DialogStyle);
    restoreDialog.show();
    Button btCertain = (Button)View.findViewById(R.id.btCertain);
    Button btCancel = (Button)View.findViewById(R.id.btCancel);
    btCancel.setOnClickListener(onRestoreCancelClick);//添加按钮点击事件
   btCertain.setOnClickListener(onRestoreCertainClick);
}

2.其他对话对话框具体看事例代码步骤相同

 工程地址:

http://download.csdn.net/detail/cx415462822/9658431

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值