• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - 如何创建没有标题的 DialogFragment?

[复制链接]
菜鸟教程小白 发表于 2022-8-1 01:20:57 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在创建一个 DialogFragment 来显示一些关于我的应用程序的帮助消息。除了一件事之外,一切正常:窗口顶部有一条黑色条纹,显示 DialogFragment,我认为它是为标题保留的,我不想使用。

这特别痛苦,因为我的自定义 DialogFragment 使用白色背景,所以这种变化太臭名昭著了,不能放在一边。

让我以更形象的方式向您展示:

enter image description here

现在我的 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" 
        androidrientation="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" 
            androidrientation="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" 
            androidrientation="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?

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



Best Answer-推荐答案


只需在您的 HelpDialog.onCreateView(...) 中添加这行代码即可

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

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

编辑

@DataGraham@Blundell在下面的评论中指出,在 onCreateDialog() 中添加对无标题窗口的请求会更安全。方法而不是 onCreateView() .这样,当您不将 fragment 用作 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;
}

关于android - 如何创建没有标题的 DialogFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15277460/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap