如何创建无标题DialogFragment?无标题、DialogFragment

2023-09-04 07:45:15 作者:爱你依旧

我创建一个DialogFragment以示对我的应用程序的一些帮助信息。一切工作正常,除了一件事...有黑色条纹的,显示DialogFragment窗口的顶部,我presume被保留为标题,这是我不想使用。

I'm creating a DialogFragment to show some help messages regarding my app. Everything works fine besides one thing...there is black stripe at the top of the window that shows the DialogFragment, that I presume is reserved for the title, something I don't want to use.

这是特别痛苦的,因为我的自定义DialogFragment使用白色背景,这样的变化是太臭名昭著的被抛在一边。

This is specially painful since my custom DialogFragment uses white background, so the change is way too notorious to be left aside.

让我告诉你这个更图形化的方式:

Let me show you this in a more graphical manner:

现在的XML code我DialogFragment如下:

Now the XML code for my DialogFragment is as follows:

<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>

编辑:包括$ C $实现DialogFragment的类C:

Including code of the class that implements the 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;
}

}

和在主要活动的创建过程:

And the creation process in the main Activity:

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

我真的不知道这个答案,一个对话框相关,适合在这里也Android:如何创建一个对话框没有标题?

如何才能摆脱这个称号领域?

How can I get rid of this title area?

推荐答案

只需添加这行code在 HelpDialog.onCreateView(...)

Just add this line of code in your HelpDialog.onCreateView(...)

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

这样你明确要求得到一个窗口而不标题:)

This way you're explicitly asking to get a window without title :)

修改

由于 @DataGraham @Blundell 指出,在下面的意见,它的安全添加的要求提供在 onCreateDialog()方法,而不是 onCreateView标题少窗()。这样你可以在你不使用你的片段为prevent ennoying NPE一个对话框

As @DataGraham and @Blundell pointed out on the comments below, it's safer to add the request for a title-less window in the onCreateDialog() method instead of onCreateView(). This way you can prevent ennoying NPE when you're not using your fragment as a 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;
}