问题
I'm trying to generate a custom dialog in Android. I create my Dialog like this:
dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);
Everythings works fine except for the title of the Dialog. Even if I don't set the title of the dialog the dialog popup has a blank space at the position of the dialog.
Is there any way to hide this part of the Dialog?
I tried it with an AlertDialog but it seems the layout is not set properly:
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.map_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
// dialog = new Dialog(this);
// dialog.setContentView(R.layout.map_dialog);
dialog = builder.create();
((TextView) dialog.findViewById(R.id.nr)).setText(number);
If I use this code I get a null Pointer Exception in the last line. The dialog is not null so the TextView I try to retrieve does not exist.
If I uncomment the part where I use the Dialog Constructor everything works fine but for the title above my dialog layout.
回答1:
You can hide the title of a dialog using:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Previous version of this answer, which is overcomplicated:
You need to use an AlertDialog. There's a good explanation on the Android Developer's site about custom dialogs.
In very short summary, you do this with code like copied below from the official website. That takes a custom layot file, inflates it, gives it some basic text and icon, then creates it. You'd show it then with alertDialog.show().
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
In response to comment:
I assume that TextView with the id nr is in the View you are inflating with View view = inflater..... If so, then you need to change just one bit: instead of dialog.findView... make it view.findView.... Then once you've done that, remember to use dialog.show(), or even builder.show() without bothering to do builder.create().
回答2:
FEATURE_NO_TITLE works when creating a dialog from scratch, as in:
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
But it doesn't work when creating an AlertDialog (or using the Builder), because it already disables the title and use a custom one internally.
I have looked at the SDK sources, and I think that it can't be worked around. So to remove the top spacing, the only solution is to create a custom dialog from scratch IMO, by using the Dialog class directly.
Also, one can do that with a style, eg in styles.xml:
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
And then:
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
回答3:
In your code add this line
requestWindowFeature(Window.FEATURE_NO_TITLE);
Or in XML use a theme
android:theme="@android:style/Theme.NoTitleBar"
XML would be a better implementation as with the code version the title bar gets created and then removed which is a waste of resource
Ok good try but it is not working. I get: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application if I want to shwo the dialog.
Change the alert dialog type to system dialog ( e.g., TYPE_SYSTEM_OVERLAY ) and see if this resolves your issue
回答4:
Use like this:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
This will remove any title bar from dialog window.
回答5:
Use below code before setcontentview :-
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
Note: You must have above code, in same order and line.
requestWindowFeature must be before the setContentView line.
回答6:
you can remove title by
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
where dialog is name of my dialog .
回答7:
In your code if you use requestWindowFeature(Window.FEATURE_NO_TITLE); be sure that it goes before dialog.setContentView(); otherwise it causes the application to crash.
回答8:
I found Three Way to do this >
1) Using requestWindowFeature
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
2) Using style (style.xml)
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
3) Using XML theme in AndroidManifest.xml
android:theme="@android:style/Theme.NoTitleBar"
回答9:
In your Custom_Dialog.java class add requestWindowFeature(Window.FEATURE_NO_TITLE)
public class Custom_Dialog extends Dialog {
protected Custom_Dialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
requestWindowFeature(Window.FEATURE_NO_TITLE); //This line
}
}
回答10:
olivierg's answer worked for me and is the best solution if creating a custom Dialog class is the route you want to go. However, it bothered me that I couldn't use the AlertDialog class. I wanted to be able to use the default system AlertDialog style. Creating a custom dialog class would not have this style.
So I found a solution (hack) that will work without having to create a custom class, you can use the existing builders.
The AlertDialog puts a View above your content view as a placeholder for the title. If you find the view and set the height to 0, the space goes away.
I have tested this on 2.3 and 3.0 so far, it is possible it doesn't work on every version yet.
Here are two helper methods for doing it:
/**
* Show a Dialog with the extra title/top padding collapsed.
*
* @param customView The custom view that you added to the dialog
* @param dialog The dialog to display without top spacing
* @param show Whether or not to call dialog.show() at the end.
*/
public static void showDialogWithNoTopSpace(final View customView, final Dialog dialog, boolean show) {
// Now we setup a listener to detect as soon as the dialog has shown.
customView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Check if your view has been laid out yet
if (customView.getHeight() > 0) {
// If it has been, we will search the view hierarchy for the view that is responsible for the extra space.
LinearLayout dialogLayout = findDialogLinearLayout(customView);
if (dialogLayout == null) {
// Could find it. Unexpected.
} else {
// Found it, now remove the height of the title area
View child = dialogLayout.getChildAt(0);
if (child != customView) {
// remove height
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
lp.height = 0;
child.setLayoutParams(lp);
} else {
// Could find it. Unexpected.
}
}
// Done with the listener
customView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
// Show the dialog
if (show)
dialog.show();
}
/**
* Searches parents for a LinearLayout
*
* @param view to search the search from
* @return the first parent view that is a LinearLayout or null if none was found
*/
public static LinearLayout findDialogLinearLayout(View view) {
ViewParent parent = (ViewParent) view.getParent();
if (parent != null) {
if (parent instanceof LinearLayout) {
// Found it
return (LinearLayout) parent;
} else if (parent instanceof View) {
// Keep looking
return findDialogLinearLayout((View) parent);
}
}
// Couldn't find it
return null;
}
Here is an example of how it is used:
Dialog dialog = new AlertDialog.Builder(this)
.setView(yourCustomView)
.create();
showDialogWithNoTopSpace(yourCustomView, dialog, true);
If you are using this with a DialogFragment, override the DialogFragment's onCreateDialog method. Then create and return your dialog like the first example above. The only change is that you should pass false as the 3rd parameter (show) so that it doesn't call show() on the dialog. The DialogFragment will handle that later.
Example:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new AlertDialog.Builder(getContext())
.setView(yourCustomView)
.create();
showDialogWithNoTopSpace(yourCustomView, dialog, false);
return dialog;
}
As I test this further I'll be sure to update with any additional tweaks needed.
回答11:
I don't know if this question is still actual, but in my case, when I switched from Dialog to DialogFragment,
requestWindowFeature(Window.FEATURE_NO_TITLE);
was not an option, but I could use
setStyle(STYLE_NO_TITLE, 0);
instead with the same result.
回答12:
Set the title to empty string using builder.
Builder builder = new AlertDialog.Builder(context);
builder.setTitle("");
...
builder.show();
回答13:
set the "gravity" attribute on the entire dialog to "center". Then you will need to override that setting to all of the child components in the dialog that you do not want centered.
回答14:
dialog=new Dialog(YourActivity.this, 1); // to make dialog box full screen with out title.
dialog.setContentView(layoutReference);
dialog.setContentView(R.layout.layoutexample);
回答15:
in XML use a theme
android:theme="@android:style/Theme.NoTitleBar"
回答16:
If we simply use the dialog without the setTitle(),then is that gonna work in removing the space of the title ?
mUSSDDialog = new AlertDialog.Builder(context).setView(dialogView)
.setPositiveButton(R.string.send_button,DialogListener)
.setNegativeButton(R.string.cancel,DialogListener)
.setCancelable(false).create();
回答17:
Think you can just use this now:
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(view)
.setTitle("")
.create()
回答18:
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
creates a title less dialog
回答19:
public static AlertDialog showAlertDialogWithoutTitle(Context context,String msg)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setMessage(msg).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return alertDialogBuilder.create();
}
回答20:
While using AlertDialog, not using setTitle() makes the title disappear
回答21:
After a bunch of hacking, I got this to work:
Window window = dialog.getWindow();
View view = window.getDecorView();
final int topPanelId = getResources().getIdentifier( "topPanel", "id", "android" );
LinearLayout topPanel = (LinearLayout) view.findViewById(topPanelId);
topPanel.setVisibility(View.GONE);
回答22:
You Can do this without using AlertDialog by defining new Class that extends from Dialog Class like this:
public class myDialog extends Dialog {
public myDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
回答23:
Here's something you can do with AlertBuilder to make the title disappear:
TextView title = new TextView(this);
title.setVisibility(View.GONE);
builder.setCustomTitle(title);
回答24:
Use this
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCancelable(true);
dialog.setContentView(R.layout.image_show_dialog_layout);
回答25:
dialog_custom .requestWindowFeature(Window.FEATURE_NO_TITLE);
this will remove title from cutsom dialog.
Note add these line before adding content.. for eg
dialog_custom = Dialog(activity!!)
dialog_custom.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog_custom.setContentView(R.layout.select_vehicle_type)
dialog_custom.setCanceledOnTouchOutside(false)
dialog_custom.setCancelable(true)
来源:https://stackoverflow.com/questions/2644134/android-how-to-create-a-dialog-without-a-title