安卓自定义Dialog(一)

来源:互联网 发布:连江有淘宝培训班吗 编辑:程序博客网 时间:2024/05/17 08:55

这个自定义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>

效果图: