没有标题的Android对话框未水平居中

没有标题的Android对话框未水平居中,第1张

概述我总是使用styles.xml或androidrequestWindowFeature(Window.FEATURE_NO_TITLE)中的android:windowNoTitle创建不带标题的自定义对话框,使其居中(垂直和水平),但是我的某些对话框不是水平居中,例如此对话框:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="h

我总是使用styles.xml或androidrequestwindowFeature(Window.FEATURE_NO_Title)中的androID:windowNoTitle创建不带标题的自定义对话框,使其居中(垂直和水平),但是我的某些对话框不是水平居中,例如此对话框:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_gravity="center"    androID:padding="20dp"androID:gravity="center"androID:background="@drawable/dialog_bg" ><include     androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    layout="@layout/loading_s"/><TextVIEw    androID:ID="@+ID/message"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_marginleft="20dp"    androID:gravity="center_vertical"    androID:text="@string/loading"    androID:textcolor="@color/dialog_text"    androID:textSize="@dimen/dialog_Title_text_size" /></linearLayout>

This is how to create dialog:

@OverrIDepublic Dialog onCreateDialog(Bundle savedInstanceState){    VIEw v = LayoutInflater.from(getActivity()).inflate(R.layout.dlg_progress, null);    Dialog dlg = new Dialog(getActivity(), R.style.My_Dialog_Style); //My_Dialog_Style contains androID:windowNoTitle = true    dlg.setContentVIEw(v);    dlg.setCanceledOntouchOutsIDe(false);       dlg.setCancelable(true);    return dlg;}

这是它在屏幕上的显示方式

如果我删除androID:windowNoTitle属性,此对话框将正确显示,因此仅在使用不带标题的对话框时才会出现问题.

有谁知道为什么会这样,以及如何使对话框始终居中显示在屏幕上?

解决方法:

使用Builder并使用setVIEw设置自定义视图时,不必删除对话框的标题,并且对话框应居中.

public Dialog onCreateDialog(Bundle savedInstanceState) {    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());    LayoutInflater inflater = getActivity().getLayoutInflater();    builder.setVIEw(inflater.inflate(R.layout.dlg_progress, null));    return builder.create();}

这与在文档中的完成方式非常相似:Creating a Custom Layout

总结

以上是内存溢出为你收集整理的没有标题的Android对话框未水平居中全部内容,希望文章能够帮你解决没有标题的Android对话框未水平居中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1211224.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存