安卓自定义Dialog(一)

这个自定义Dialog主要是提醒用户一些信息:该环境没有网,登录账号是提示密码错误....

话不多说直接上代码:


一.实现功能的.java类

public static Dialog CreatDialog(Context context, String s,
                                     View.OnClickListener listener) {
        final Dialog dialog = new Dialog(context, R.style.dialog);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();// 屏幕属性类
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_notile, null);
        TextView tv = (TextView) view.findViewById(R.id.notile_tv);
        tv.setText(s);
        Button bt = (Button) view.findViewById(R.id.notile_bt);
        if (null == listener) {
            bt.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        } else {
            bt.setOnClickListener(listener);
        }
        dialog.setContentView(view, new LinearLayout.LayoutParams(
                dm.widthPixels * 4 / 5, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(false);//设置返回键无法取消dialog

         /*设置成系统级别的弹框,可以在service弹框而不受acivity的局限性*/
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        return dialog;
    }

context是但前activity,s是你要输入的文字内容,listener是你点击确定后做的一些操作,如果没有接设置为null。

R.style.dialog
  <!-- Dialog样式 -->
    <style name="dialog" parent="android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!-- Dialog的windowFrame框为无 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 切记一定要设置为透明状态,不然有阴影 -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 背景是否模糊显示 -->
        <item name="android:backgroundDimAmount">0.4</item>
    </style>


一定要设置
parent="android:Theme.Dialog"
不然dialog会有阴影,一些相关属性


二.布局.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/popwindow_top">

        <TextView
            android:id="@+id/notile_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/lcdBtnmargin"
            android:gravity="center_horizontal"
            android:text="adsdadassadasdasadadasddasdasasdsadadad"
            android:textColor="@color/blueSky"
            android:textSize="@dimen/lcdtvContent"/>
    </LinearLayout>

    <ImageView style="@style/iv"/>

    <Button
        android:id="@+id/notile_bt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/popwindow_bottom"
        android:text="@android:string/ok"
        android:textColor="@drawable/textcolor"
        android:textSize="@dimen/lcdtvTitle"/>

</LinearLayout>

先给你们@drawable/popwindow_bottom的背景你们再去推里剩下的背景 吐舌头

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

    <item android:state_pressed="true"><shape>
            <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" />

            <solid android:color="@color/blueSky" />
        </shape></item>
    <item android:state_focused="true"><shape>
            <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" />

            <solid android:color="@color/blueSky" />
        </shape></item>
    <item><shape>

            <solid android:color="@color/white" />

            <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" />
        </shape></item>

</selector>

效果图:






























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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值