CustomDialogFragment.java

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
public class CustomDialogFragment extends DialogFragment {
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = new Dialog(getActivity());
		// タイトル非表示
		dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		// フルスクリーン
		dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
		dialog.setContentView(R.layout.dialog_custom);
		// 背景を透明にする
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		// OK ボタンのリスナ
		dialog.findViewById(R.id.positive_button).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		// Close ボタンのリスナ
		dialog.findViewById(R.id.close_button).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		return dialog;
	}
}

bg_dialog_title.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:topLeftRadius="5dp" android:topRightRadius="5dp"/>
    <solid android:color="#9acd32"/>
</shape>

bg_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="5dp"/>
    <stroke android:width="2dp" android:color="#9acd32"/>
    <gradient android:startColor="#ffffff" android:endColor="#dcdcdc" android:angle="90"/>
</shape>

bt_dialog_close.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@android:color/white"/>
    <stroke
        android:width="3dp"
        android:color="#9acd32"
        />
</shape>

bt_dialog_positive.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="5dp" />
    <solid android:color="#9acd32" />
</shape>

사용하기

호출하는 Actitivy는 FragmentActivity를 계승해야하며 아래와 같이 표시할 수 있습니다.
CustomDialogFragment cDialog = new CustomDialogFragment();
cDialog.show(getSupportFragmentManager(), "tag");

Thanks to

http://dev.classmethod.jp/smartphone/android/android-tips-45-custom-dialog/ ]]>

Related Posts