LoginSignup
50
49

More than 5 years have passed since last update.

透過ダイアログの作り方

Last updated at Posted at 2014-09-03

1.透過ダイアログ用のテーマを作成する

styles.xml
<style name="TransparentDialogTheme" parent="@android:style/Theme.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

2.背景色を定義する(背景画像を当てない場合)

styles.xml
<color name="dialogbg">#a0909090</color>

先頭の二桁を00にすると完全な透明になります。

3.Dialog用のレイアウトxmlを作成する

・レイアウト背景に、2.で作成した背景色を設定するか、半透明の画像を当てる

mydialog.xml
android:background="@color/dialogbg"

or

android:background="@drawable/dialogbg_image"

4.DialogFragmentを継承したダイアログクラスを作成する

MyDialog.java
public class MyDialog extends DialogFragment {

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new Dialog(getActivity(),
        R.style.TransparentDialogTheme);
    // ダイアログの背景を完全に透過。
    dialog.getWindow().setBackgroundDrawable(
        new ColorDrawable(Color.TRANSPARENT));
    // フルスクリーンでダイアログを表示。
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    dialog.setCanceledOnTouchOutside(false); // キャンセル不可の場合
    setCancelable(false); // キャンセル不可の場合
    return dialog;

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View content = inflater.inflate(R.layout.mydialog, null);
    return content;
  }
}

5.表示する

MainActivity.java
MyDialog dialog = new MyDialog();
dialog.show(getFragmentManager(), "tag");
50
49
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50
49