Android 如何创建没有标题的DialogFragment?
我正在创建一个DialogFragment来显示一些关于我的应用程序的帮助消息。除了一件事之外,一切都很好:窗口顶部有一条黑色条纹,显示DialogFragment,我想这是为标题保留的,我不想使用它 这是特别痛苦的,因为我的自定义DialogFragment使用白色背景,所以这个更改太臭名昭著了,不能放在一边 让我以更形象的方式向您展示: 现在,我的DialogFragment的XML代码如下所示:Android 如何创建没有标题的DialogFragment?,android,android-dialogfragment,Android,Android Dialogfragment,我正在创建一个DialogFragment来显示一些关于我的应用程序的帮助消息。除了一件事之外,一切都很好:窗口顶部有一条黑色条纹,显示DialogFragment,我想这是为标题保留的,我不想使用它 这是特别痛苦的,因为我的自定义DialogFragment使用白色背景,所以这个更改太臭名昭著了,不能放在一边 让我以更形象的方式向您展示: 现在,我的DialogFragment的XML代码如下所示: <ScrollView xmlns:android="http://schemas.a
<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>
以及主要活动中的创建过程:
/**
* Shows the HelpDialog Fragment
*/
private void showHelpDialog() {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
HelpDialog helpDialog = new HelpDialog();
helpDialog.show(fm, "fragment_help");
}
我真的不知道这个与对话有关的答案是否也适用于这里
如何删除此标题区域?只需在您的
帮助对话框中添加这行代码。onCreateView(…)
这样,您就明确要求获得一个没有标题的窗口:)
编辑 正如
@DataGraham
和@Blundell
在下面的评论中指出的那样,在onCreateDialog()
方法中添加对无标题窗口的请求,而不是onCreateView()
。这样,当您不将片段用作对话框时,就可以防止使NPE ennoying:
@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;
}
对话框片段具有setStyle方法,应在创建视图之前调用该方法。也可以使用相同的方法设置对话框的样式
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;
}
将样式设置为“主题”对话框操作栏:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
}
从容不迫
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
当然,您也可以使用方便的方法getDialog().requestWindowFeature(Window.FEATURE\u NO\u TITLE)
。您可能希望在onCreateDialog中执行此操作,以防您的片段显示为非对话框。@a.bertucci您是最好的!但是,如果您将该片段作为普通片段添加,则getDialog
将返回一个npeth,这将缩小我的对话框的宽度。此问题的任何解决方法?@clocksmith是对话框布局中根视图的android:layout\u width
属性,设置为wrap\u content
?此showHelpDialog方法从FragmentActivity或Activity class???+1调用。。。我想这是我第一次在阅读关于幽默感的问题lol+1时大笑。你对这个问题的图形解释正是我对这个问题的感受!Max,我试着使用SetType(STYLE_NO_TITLE,0)。但结果是,我的对话框被剪切为宽度的1/4大小,因此对话框中的消息并没有全部显示出来。从android源代码来看,我认为这两种方式在代码中实际上是相同的。我也遇到了同样的问题,它的大小也减少了1/4:(.还没有解决它。这是最好的方法。@Sam你说得对,窗口样式在onSaveInstanceState中保持并在以后恢复。我仍然认为在onCreate()中初始化所有内容是更好的做法。)一般来说,布局根视图的所有直接子视图都必须是一个匹配的宽度。然后你会得到一个正常宽度的对话框。sd.setStyle(DialogFragment.STYLE\u normal,android.R.STYLE.Theme\u Holo\u dialog\u NoActionBar)我会说,经过这么多的尝试,这是唯一一个有效的对话框。在三星Galaxy S4上运行良好。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
}
FragmentManager manager = getSupportFragmentManager();
SettingsDialog sd = new SettingsDialog();
sd.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
sd.show(manager, "settings_dialog");
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;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}