Android Dialog風格彈出框的Activity

cswhale發表於2019-01-22

1.styles.xml 中

<style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
 </style>

1.1style配置項參考:

<style name="dialogstyle" parent="@android:style/Theme.Dialog">  
        <!--設定dialog的背景-->  
        <item name="android:windowBackground">@android:color/transparent</item>  
        <!--設定Dialog的windowFrame框為無-->  
        <item name="android:windowFrame">@null</item>  
        <!--設定無標題-->  
        <item name="android:windowNoTitle">true</item>  
        <!--是否浮現在activity之上-->  
        <item name="android:windowIsFloating">true</item>  
        <!--是否半透明-->  
        <item name="android:windowIsTranslucent">true</item>  
        <!--設定視窗內容不覆蓋-->  
        <item name="android:windowContentOverlay">@null</item>  
        <!--設定動畫,在這裡使用讓它繼承系統的Animation.Dialog-->  
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>  
        <!--背景是否模糊顯示-->  
        <item name="android:backgroundDimEnabled">true</item>  
    </style>  

2.AndroidManifest.xml 中

<activity
            android:name="com.demo.application.DialogActivity"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait" 
            android:theme="@style/dialog"/>

3.彈框佈局檔案: dialog_activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="center">

    <LinearLayout
        android:id="@+id/dialog_layout"
        android:layout_width="wrap_content"
        android:layout_height="120dp"
        android:layout_gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/dialog_radius_white_shape"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tip_textview"
            android:layout_width="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_height="80dp"
            android:gravity="center"
            android:text="@string/vpn_yc_tip"
            android:textColor="#9D9D9D"
            android:textSize="14sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#8E8E8E" />

        <TextView
            android:id="@+id/comfirm_textview"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="確認"
            android:textColor="#0080FF"
            android:textSize="16sp" />
    </LinearLayout>

</RelativeLayout>

4.圓角白色背景Shape

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="20dp" />
    <solid android:color="@android:color/white" />
</shape>

5.彈框頁面: DialogActivity.java

package com.demo.func.application;

import com.demo.func.R;
import com.demo.func.R.id;
import com.demo.func.R.layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DialogActivity extends Activity {
    public static DialogActivity instance = null;
    private TextView comfirm_textview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_activity_layout);
        this.setFinishOnTouchOutside(false);//設定視窗周圍觸控不消失
        getWindow().setDimAmount(0f);//設定視窗周圍透明
        instance = this;
        initView();
    }
    private void initView() {
        comfirm_textview = (TextView) this.findViewById(R.id.comfirm_textview);
        comfirm_textview.setClickable(true);
        comfirm_textview.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View paramView) {
               //todo
                finish();
            }
        });
    }
}

相關文章