Android自定义圆角dialog

MyDialog.java:

package com.weijie.app_user.activity.login;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MyDialog extends Dialog {
    
    private static int default_width = 160; //默认宽度
    private static int default_height = 120;//默认高度
    
    public MyDialog(Context context, View layout, int style) {
        this(context, default_width, default_height, layout, style);
    }
    
    public MyDialog(Context context, int width, int height, View layout, int style) {
        super(context, style);
        setContentView(layout);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width=width;
        params.height=height;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }
    
}

cztest_main.xml:

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

	<Button 
		android:id="@+id/mybut"
		android:text="测试"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"/>
</LinearLayout>

CzTestActivity.java:

package com.weijie.app_user.activity.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

import com.weijie.app_user.R;
import com.weijie.app_user.activity.login.MyDialog;

public class CzTestActivity extends Activity {

	private Button mybut = null; // 定义按钮
	private PopupWindow popWin = null; // 弹出窗口
	private View popView = null; // 保存弹出窗口布局

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.cztest_main); // 调用布局管理器
		this.mybut = (Button) super.findViewById(R.id.mybut); // 取得按钮
		this.mybut.setOnClickListener(new OnClickListenerImpl()); // 设置事件类
	}

	// 新版本
	private class OnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View view) {
			View view1 = getLayoutInflater().inflate(R.layout.login_newversion_dialog,
					null);
			MyDialog myDialog = new MyDialog(CzTestActivity.this, 300, 200, view1,
					R.style.self_dialog);
			// 设置对话框显示的View
			myDialog.show();
		}
	}

	// 选择社交平台弹出窗
	/*
	 * private class OnClickListenerImpl implements OnClickListener {
	 * 
	 * @Override public void onClick(View view) { LayoutInflater inflater =
	 * LayoutInflater.from(CzTestActivity.this); // 取得LayoutInflater对象
	 * CzTestActivity.this.popView = inflater.inflate(
	 * R.layout.login_social_platform_dialog, null); // 读取布局管理器
	 * CzTestActivity.this.popWin = new PopupWindow(popView,
	 * LayoutParams.MATCH_PARENT, 380, true); // 实例化PopupWindow //
	 * 设置PopupWindow的弹出和消失效果
	 * 
	 * CzTestActivity.this.popWin .setAnimationStyle(R.style.popupAnimation);
	 * 
	 * CzTestActivity.this.popWin.showAtLocation( CzTestActivity.this.mybut,
	 * Gravity.BOTTOM, 0, 0); // 显示弹出窗口 } }
	 */

}

login_newversion_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/rounded_corners_view"
    android:orientation="vertical" 
    android:fadingEdge="none"
    style="@android:style/Theme.Dialog">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="@drawable/round_corner_view_title"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提    示"
            android:textColor="@color/black"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发现新版本"
            android:textColor="#333"
            android:textSize="18sp" />
    </LinearLayout>

    <!-- 版本提示 -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="v3.5.0"
            android:textColor="#717171"
            android:textSize="18sp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/setting_arrow" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="v3.5.1"
            android:textColor="#ff6600"
            android:textSize="18sp" />
    </LinearLayout>

    <!-- 按钮 -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
         android:layout_marginBottom="15dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/textview_style_cancle_gray"
            android:layout_marginRight="20dp"
            android:text="取    消"
            android:textColor="@color/white"
            android:textSize="18sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/textview_style_update_yellow"
            android:text="更    新"
            android:textColor="@color/white"
            android:textSize="18sp" />
    </LinearLayout>

</LinearLayout>

rounded_corners_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
	<solid android:color="#ffffff" />
	
	<stroke android:width="1dp" android:color="#797979" />
	
	<corners android:radius="10dp" />
	
	<!-- <padding android:left="5dp" android:top="5dp" 
		android:right="5dp" android:bottom="5dp" /> -->
</shape>

round_corner_view_title.xml:

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

    <solid android:color="#DBD9DA" />

    <stroke
        android:width="1dp"
        android:color="#797979" />

    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

    <!--
         <padding android:left="5dp" android:top="5dp" 
		android:right="5dp" android:bottom="5dp" />
    -->

</shape>

<style name="self_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:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.6</item>
    </style>

Android自定义圆角dialog

相关推荐