1. 程式人生 > >安卓自定義Dialog(一)

安卓自定義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>

效果圖: