Android Dialog风格弹出框的Activity

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cswhale/article/details/86596943

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();
            }
        });
    }
}

展开阅读全文

没有更多推荐了,返回首页