【问题标题】:How to create a DialogFragment without title?如何创建没有标题的 DialogFragment?
【发布时间】:2023-03-31 20:56:01
【问题描述】:

我正在创建一个 DialogFragment 来显示一些关于我的应用程序的帮助消息。除了一件事之外,一切都很好:窗口顶部有一条黑色条纹显示 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;
    }

}

以及主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 to create a Dialog without a title?

我怎样才能摆脱这个标题区?

【问题讨论】:

  • 这个showHelpDialog方法是从FragmentActivity还是Activity类调用的??
  • +1...我想这是我第一次在阅读问题时笑了哈哈
  • 幽默感+1。您对问题的图形解释正是我对这个问题的感受!

标签: android android-dialogfragment


【解决方案1】:

只需将这行代码添加到您的HelpDialog.onCreateView(...)

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

这样你就明确要求得到一个没有标题的窗口:)


编辑

正如@DataGraham@Blundell 在下面的cmets 中指出的那样,在onCreateDialog() 方法中添加对无标题窗口的请求而不是onCreateView() 更安全。这样,当您不将片段用作 Dialog 时,您可以防止烦人的 NPE:

@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;
}

【讨论】:

  • 当然你也可以使用方便的方法getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE)...
  • 可能希望在 onCreateDialog 中执行此操作,以防您的片段显示为对话框。
  • @a.bertucci 你是最棒的!但是,如果您将该片段添加为普通片段,getDialog 将返回一个 NPE
  • 这会缩小我的对话框的宽度。这个问题有什么解决方法吗?
  • @clocksmith 是设置为wrap_content 的对话框布局中根视图的android:layout_width 属性?
【解决方案2】:

对话框片段有setStyle方法,应该在视图创建之前调用Java Doc。对话框的样式也可以用同样的方法设置

public static MyDialogFragment newInstance() {
        MyDialogFragment mDialogFragment = new MyDialogFragment();
        //Set Arguments here if needed for dialog auto recreation on screen rotation
        mDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
        return mDialogFragment;
}

【讨论】:

  • Max,我尝试使用 setStype(STYLE_NO_TITLE, 0)。但是结果是我的对话框被剪成宽度的1/4大小,所以对话框中的消息并没有全部显示出来。
  • 我认为这两种方式在代码中实际上是相同的,从查看android源代码来看。我也遇到了同样的问题,它的尺寸也缩小了 1/4 :(。还没有解决。
  • 这是最好的方法。
  • @Sam 你说得对,Window 样式在 onSaveInstanceState 中保持不变,然后恢复。我仍然认为一般来说在 onCreate() 中初始化所有内容是一种更好的做法。
  • 布局的根视图的所有直接子级必须是 match_parant 的宽度。然后你会得到一个正常宽度的对话框。
【解决方案3】:
FragmentManager manager = getSupportFragmentManager();
SettingsDialog sd = new SettingsDialog();
sd.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
sd.show(manager, "settings_dialog");

【讨论】:

  • sd.setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar) 我会说
  • 天啊,经过这么多的试验,这是唯一有效的作品。
【解决方案4】:

尝试简单的方法

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, 0);
}

【讨论】:

  • 在三星 Galaxy S4 上运行良好。
【解决方案5】:

将样式设置为 Theme_Holo_Dialog_NoActionBar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
}

【讨论】:

    【解决方案6】:
    public class LoginDialog extends DialogFragment {   
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.login_dialog, null);
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            return view;
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 2011-02-08
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      相关资源
      最近更新 更多