Jan*_*usz 263 android dialog android-layout
我正在尝试在Android中生成自定义对话框.我像这样创建我的Dialog:
dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);
Run Code Online (Sandbox Code Playgroud)
除了Dialog的标题之外,Everythings工作正常.即使我没有设置对话框的标题,对话框弹出窗口在对话框的位置也有一个空格.
有没有办法隐藏Dialog的这一部分?
我尝试使用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);
Run Code Online (Sandbox Code Playgroud)
如果我使用这段代码,我会在最后一行得到一个空指针异常.该对话框不为null,因此我尝试检索的TextView不存在.
如果我取消注释我使用Dialog构造函数的部分,一切正常,但对于我的对话框布局上方的标题.
oli*_*erg 585
从头开始创建对话框时,FEATURE_NO_TITLE有效,如:
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
但是在创建AlertDialog(或使用Builder)时它不起作用,因为它已经禁用了标题并在内部使用自定义标题.
我查看了SDK源代码,我认为它无法解决.因此,要删除顶部间距,唯一的解决方案是直接使用Dialog类从头开始创建自定义对话框IMO.
此外,可以使用样式执行此操作,例如在styles.xml中:
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后:
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
Run Code Online (Sandbox Code Playgroud)
Ste*_*ley 202
您可以使用以下方法隐藏对话框的标题:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
此答案的先前版本过于复杂:
你需要使用一个AlertDialog
.关于自定义对话框,Android Developer的网站上有一个很好的解释.
在非常简短的总结中,您可以使用以下从官方网站复制的代码执行此操作.这需要一个自定义的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();
Run Code Online (Sandbox Code Playgroud)
回应评论:
我假设带有id的TextView nr
在你正在膨胀的视图中View view = inflater....
.如果是这样,那么你需要改变一点:而不是dialog.findView...
改变它view.findView...
.然后,一旦你完成了,记得使用dialog.show(),甚至builder.show(),而不必费心去做builder.create().
Don*_*rty 67
在您的代码中添加此行
requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
或者在XML中使用主题
android:theme="@android:style/Theme.NoTitleBar"
Run Code Online (Sandbox Code Playgroud)
XML将是一个更好的实现,因为标题栏被创建然后被删除,这是浪费资源的代码版本
好的尝试,但它不起作用.我得到:android.view.WindowManager $ BadTokenException:无法添加窗口 - 如果我想要对话框,则令牌null不适用于应用程序.
将警报对话框类型更改为系统对话框(例如,TYPE_SYSTEM_OVERLAY)并查看是否可以解决您的问题
小智 61
使用这样:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
这将从对话框窗口中删除任何标题栏.
dug*_*ggu 58
使用以下代码setcontentview
: -
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
Run Code Online (Sandbox Code Playgroud)
注意:您必须具有相同的顺序和行的上述代码.
requestWindowFeature
必须在 setContentView行之前.
小智 38
你可以删除标题
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
对话框是我的对话框的名称.
Szy*_*ski 29
在您的代码中,如果您使用,请requestWindowFeature(Window.FEATURE_NO_TITLE);
确保它在之前,dialog.setContentView();
否则会导致应用程序崩溃.
M_K*_*M_K 10
在您的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
}
}
Run Code Online (Sandbox Code Playgroud)
Nir*_*ara 10
我找到了三种方法来做到这一点>
1)使用requestWindowFeature
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
2)使用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);
Run Code Online (Sandbox Code Playgroud)
3)在AndroidManifest.xml中使用XML主题
android:theme="@android:style/Theme.NoTitleBar"
Run Code Online (Sandbox Code Playgroud)
olivierg的答案对我有用,如果创建自定义Dialog类是您想要的路线,那么它是最好的解决方案.但是,我无法使用AlertDialog类.我希望能够使用默认的系统AlertDialog样式.创建自定义对话框类将不具有此样式.
所以我找到了一个无需创建自定义类就可以工作的解决方案(hack),您可以使用现有的构建器.
AlertDialog将View放在内容视图上方作为标题的占位符.如果找到视图并将高度设置为0,则空间消失.
到目前为止,我已经在2.3和3.0上对此进行了测试,但它可能无法在每个版本上运行.
以下是两种辅助方法:
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
以下是如何使用它的示例:
Dialog dialog = new AlertDialog.Builder(this)
.setView(yourCustomView)
.create();
showDialogWithNoTopSpace(yourCustomView, dialog, true);
Run Code Online (Sandbox Code Playgroud)
如果您使用DialogFragment,请覆盖DialogFragment的onCreateDialog
方法.然后像上面的第一个例子一样创建并返回对话框.唯一的变化是你应该传递false作为第三个参数(show),这样它就不会在对话框上调用show().DialogFragment稍后会处理.
例:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new AlertDialog.Builder(getContext())
.setView(yourCustomView)
.create();
showDialogWithNoTopSpace(yourCustomView, dialog, false);
return dialog;
}
Run Code Online (Sandbox Code Playgroud)
当我进一步测试时,我将确保更新所需的任何额外调整.
我不知道这个问题是否仍然存在,但就我而言,当我从Dialog切换到DialogFragment时,
requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
不是一个选择,但我可以使用
setStyle(STYLE_NO_TITLE, 0);
Run Code Online (Sandbox Code Playgroud)
相反,结果相同.
归档时间: |
|
查看次数: |
215009 次 |
最近记录: |