タイトルなしでDialogFragmentを作成するにはどうすればいいですか? 質問する

タイトルなしでDialogFragmentを作成するにはどうすればいいですか? 質問する

アプリに関するヘルプ メッセージを表示するために DialogFragment を作成しています。1 つを除いてすべて正常に動作します。ウィンドウの上部に DialogFragment を表示する黒いストライプがありますが、これはタイトル用に予約されているものと思われますが、使用したくないものです。

私のカスタム DialogFragment は白い背景を使用しているため、この変更は無視できないほど重大なものであり、特に問題となります。

これをもっとグラフィカルな方法で示してみましょう。

ここに画像の説明を入力してください

DialogFragment の XML コードは次のようになります。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/holding" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:background="@drawable/dialog_fragment_bg"
        >
        <!-- Usamos un LinearLayout para que la imagen y el texto esten bien alineados -->
        <LinearLayout
            android:id="@+id/confirmationToast" 
            android:orientation="horizontal" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            >

            <TextView android:id="@+id/confirmationToastText" 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" 
            android:text="@string/help_dialog_fragment"
            android:textColor="#AE0000"
            android:gravity="center_vertical"
            />

        </LinearLayout>
        <LinearLayout
            android:id="@+id/confirmationButtonLL" 
            android:orientation="horizontal" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            >    
            <Button android:id="@+id/confirmationDialogButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginBottom="60dp"
                android:background="@drawable/ok_button">
            </Button>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

DialogFragment を実装するクラスのコードは次のとおりです。

public class HelpDialog extends DialogFragment {

    public HelpDialog() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the XML view for the help dialog fragment
        View view = inflater.inflate(R.layout.help_dialog_fragment, container);
        TextView text = (TextView)view.findViewById(R.id.confirmationToastText);
        text.setText(Html.fromHtml(getString(R.string.help_dialog_fragment)));
        //get the OK button and add a Listener
        ((Button) view.findViewById(R.id.confirmationDialogButton)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 // When button is clicked, call up to owning activity.
                HelpDialog.this.dismiss();
             }
         });
        return view;
    }

}

メインアクティビティでの作成プロセス:

/**
 * Shows the HelpDialog Fragment
 */
private void showHelpDialog() {
    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    HelpDialog helpDialog = new HelpDialog();
    helpDialog.show(fm, "fragment_help");
}

ダイアログに関連するこの回答がここにも当てはまるかどうかはわかりませんAndroid: タイトルなしでダイアログを作成するにはどうすればよいですか?

このタイトル領域を削除するにはどうすればいいでしょうか?

ベストアンサー1

このコード行をHelpDialog.onCreateView(...)

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

この方法では、タイトルのないウィンドウを取得するように明示的に要求しています :)


編集

@DataGrahamが以下のコメントで指摘しているように、ではなく メソッド@Blundellにタイトルのないウィンドウの要求を追加する方が安全です。 この方法では、フラグメントを として使用していない場合に、煩わしい NPE を防ぐことができます。onCreateDialog()onCreateView()Dialog

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
  Dialog dialog = super.onCreateDialog(savedInstanceState);

  // request a window without the title
  dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  return dialog;
}

おすすめ記事