自定义Dialog

1、创建dialog的布局文件,在layout下,dialog.xml


2、最主要的,是自定义的Style,我们自定义一个式样,用来改变默认的Dialog样式

在values文件夹下新建一个styles.xml文件,如下。。。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@drawable/dialog_bg</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
</resources>

3、重写Dialog类


public class MyDialog extends Dialog {

    Context context;
    public MyDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
    }
    public MyDialog(Context context, int theme){
        super(context, theme);
        this.context = context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog);
    }

}

4、创建Dialog,即MyDialog


Dialog dialog = new MyDialog(MyDialogActivity.this,R.style.MyDialog);

dialog.show();


附:

1、点击按钮之后如何退出dialog?可以监听按钮,

dialog.cancel();


2、修改按钮的点击效果

用selector即可

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- pressed -->
    <item android:state_pressed="true"><shape android:shape="rectangle">
            <gradient android:angle="270" android:endColor="#ff8c00" android:startColor="#ff8c00" />
        </shape></item>
    <!-- focus -->
    <item android:state_focused="true"><shape>
            <gradient android:angle="270" android:endColor="#ffc2b7" android:startColor="#ffc2b7" />
        </shape></item>
    <!-- default -->
    <item><shape>
            <gradient android:angle="0" android:endColor="#94d6da" android:startColor="#94d6da" />
        </shape></item>

</selector>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值