本文翻译自:How to create a DialogFragment without title?
I'm creating a DialogFragment to show some help messages regarding my app. 我正在创建一个DialogFragment来显示有关我的应用程序的一些帮助消息。 Everything works fine besides one thing: There is a 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 a white background, so the change is way too notorious to be left aside. 这是特别痛苦的,因为我的自定义DialogFragment使用白色背景,所以这种变化太臭名昭着,不能放在一边。
Let me show you this in a more graphical manner: 让我以更加图形化的方式向您展示:
Now the XML code for my DialogFragment is as follows: 现在我的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>
And the code of the class that implements the DialogFragment: 以及实现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");
}
I really don't know if this answer, related with a Dialog, fits here also Android: How to create a Dialog without a title? 我真的不知道这个与Dialog相关的答案是否适合Android:如何创建没有标题的Dialog?
How can I get rid of this title area? 我怎样才能摆脱这个标题区域?
#1楼
参考:https://stackoom.com/question/126Me/如何创建没有标题的DialogFragment
#2楼
Just add this line of code in your HelpDialog.onCreateView(...)
只需在HelpDialog.onCreateView(...)
添加这行代码
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
This way you're explicitly asking to get a window without title :) 这样你明确要求得到一个没有标题的窗口:)
EDIT 编辑
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()
. 正如@DataGraham
和@Blundell
在下面的评论中指出的那样,在onCreateDialog()
方法而不是onCreateView()
添加无标题窗口的请求更安全。 This way you can prevent ennoying NPE when you're not using your fragment as a Dialog
: 这样,当您不将片段用作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;
}
#3楼
Dialog fragment has setStyle method, which should be called before view creation Java Doc . Dialog片段有setStyle方法,应该在视图创建Java Doc之前调用。 Also style of the dialog can be set with the same method 也可以使用相同的方法设置对话框的样式
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;
}
#4楼
Set the style to Theme_Holo_Dialog_NoActionBar: 将样式设置为Theme_Holo_Dialog_NoActionBar:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
}
#5楼
FragmentManager manager = getSupportFragmentManager();
SettingsDialog sd = new SettingsDialog();
sd.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
sd.show(manager, "settings_dialog");
#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;
}
}