Android:如何创建一个没有标题的对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2644134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to create a Dialog without a title?
提问by Janusz
I'm trying to generate a custom dialog in Android. I create my Dialog like this:
我正在尝试在 Android 中生成自定义对话框。我像这样创建我的对话框:
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:
我用 AlertDialog 尝试过,但似乎布局设置不正确:
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.
如果我使用这段代码,我会在最后一行得到一个空指针异常。该对话框不为空,因此我尝试检索的 TextView 不存在。
如果我取消注释使用对话框构造函数的部分,一切正常,但对于对话框布局上方的标题。
采纳答案by Steve Haley
You can hide the title of a dialog using:
您可以使用以下方法隐藏对话框的标题:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
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.
您需要使用一个AlertDialog
. Android Developer's site 上有一个关于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()
.
简而言之,您可以使用以下从官方网站复制的代码来执行此操作。这需要一个自定义的layot文件,对其进行充气,给它一些基本的文本和图标,然后创建它。然后你会用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().
我假设带有 id 的 TextViewnr
位于您正在使用的视图中View view = inflater....
。如果是这样,那么您只需要更改一点:而不是dialog.findView...
make it view.findView...
。然后一旦你这样做了,记得使用 dialog.show() ,甚至是 builder.show() 而不必费心去做 builder.create() 。
回答by olivierg
FEATURE_NO_TITLE works when creating a dialog from scratch, as in:
FEATURE_NO_TITLE 在从头开始创建对话框时起作用,如下所示:
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.
但是在创建 AlertDialog(或使用 Builder)时它不起作用,因为它已经禁用了标题并在内部使用了自定义标题。
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.
我查看了 SDK 源代码,我认为它无法解决。因此,要删除顶部间距,唯一的解决方案是直接使用 Dialog 类从头开始 IMO 创建自定义对话框。
Also, one can do that with a style, eg in styles.xml:
此外,您可以使用样式来做到这一点,例如在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);
回答by Donal Rafferty
In your code add this line
在您的代码中添加这一行
requestWindowFeature(Window.FEATURE_NO_TITLE);
Or in XML use a theme
或者在 XML 中使用主题
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
XML 将是一个更好的实现,因为代码版本的标题栏被创建然后被删除,这是一种资源浪费
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.
好的尝试,但它不工作。我得到: android.view.WindowManager$BadTokenException: Unable to add window -- 如果我想显示对话框,则令牌 null 不适用于应用程序。
Change the alert dialog type to system dialog ( e.g., TYPE_SYSTEM_OVERLAY ) and see if this resolves your issue
将警报对话框类型更改为系统对话框(例如 TYPE_SYSTEM_OVERLAY ),看看这是否能解决您的问题
回答by alok tiwari
Use like this:
像这样使用:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
This will remove any title bar from dialog window.
这将从对话框窗口中删除任何标题栏。
回答by duggu
Use below code before setcontentview
:-
在之前使用以下代码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 beforethe setContentView line.
注意:您必须具有以上代码,顺序和行相同。
requestWindowFeature
必须在setContentView 行之前。
回答by shailendra
you can remove title by
您可以通过以下方式删除标题
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
where dialog is name of my dialog .
其中 dialog 是我的对话框的名称。
回答by Szymon Morawski
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.
在您的代码中,如果您使用,请requestWindowFeature(Window.FEATURE_NO_TITLE);
确保它在之前,dialog.setContentView();
否则会导致应用程序崩溃。
回答by Nirav Ranpara
I found Three Way to do this >
我找到了三种方法来做到这一点>
1) Using requestWindowFeature
1) 使用 requestWindowFeature
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
2) Using style (style.xml)
2)使用样式(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
3) 在 AndroidManifest.xml 中使用 XML 主题
android:theme="@android:style/Theme.NoTitleBar"
回答by M_K
In your Custom_Dialog.java class add requestWindowFeature(Window.FEATURE_NO_TITLE)
在您的 Custom_Dialog.java 类中添加 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
}
}
回答by cottonBallPaws
olivierg's answerworked 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.
olivierg 的答案对我有用,如果创建自定义 Dialog 类是您想要的路线,则它是最佳解决方案。但是,我无法使用 AlertDialog 类让我感到困扰。我希望能够使用默认的系统 AlertDialog 样式。创建自定义对话框类不会有这种风格。
So I found a solution (hack) that will work without having to create a custom class, you can use the existing builders.
所以我找到了一个无需创建自定义类即可工作的解决方案(hack),您可以使用现有的构建器。
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.
AlertDialog 在您的内容视图上方放置一个视图作为标题的占位符。如果找到视图并将高度设置为 0,则空间消失。
I have tested this on 2.3 and 3.0 so far, it is possible it doesn't work on every version yet.
到目前为止,我已经在 2.3 和 3.0 上对此进行了测试,它可能不适用于每个版本。
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.
如果您将它与 DialogFragment 一起使用,请覆盖 DialogFragment 的onCreateDialog
方法。然后像上面的第一个示例一样创建并返回您的对话框。唯一的变化是您应该将 false 作为第三个参数 (show) 传递,以便它不会在对话框中调用 show()。DialogFragment 将在稍后处理。
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.
当我进一步测试时,我一定会更新所需的任何其他调整。