【问题标题】:Android dialog without title not centered horizontally没有标题的Android对话框不水平居中
【发布时间】:2015-06-23 01:14:44
【问题描述】:

我总是使用styles.xmlrequestWindowFeature(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>

这是创建对话框的方法:

@Override
public 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 属性,此对话框将正确显示,因此仅在使用没有标题的对话框时才会出现问题。

有谁知道为什么会发生这种情况以及如何使对话框始终位于屏幕中心?

【问题讨论】:

  • 如何创建对话框?这是DialogFragment吗?
  • 是的,这是一个 DialogFragment,请参阅更新以了解我如何创建 Dialog。

标签: android dialog


【解决方案1】:

你试过看这个帖子吗?

How to align custom dialog centre in android ?

 android:layout_gravity="center" 

看起来只是布局更改,或者尝试使用 relativeLayout 或 LinearLayout 而不是 FrameLayout

【讨论】:

  • 你可以看到我已经使用了android:layout_gravity="center" 。我的许多对话框都使用LinearLayout 作为对话框容器,但它显示正确,所以我认为问题不是由 LinearLayout 引起的。
【解决方案2】:

当您使用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

【讨论】:

  • 谢谢,但我想创建一个完全自定义的复杂对话框,所以我需要使用 Dialog 而不仅仅是 AlertDialog.Builder。
  • @Wayne 然后你可以使用onCreateView,就像使用普通片段一样,而不是onCreateDialog。要删除标题,请使用setStyle(STYLE_NO_TITLE, 0)
  • @Wayne 抱歉,不知道。
【解决方案3】:

我相信您正在降低对话框的最小宽度属性。可以找到

<item type="dimen" name="dialog_min_width_major">65%</item>

在 Android 的框架中。它取决于您正在查看的values 文件夹,因此它会根据密度、方向等而有所不同。

您可以在您的样式中覆盖此值。如果您将其设置为绝对小于您的对话框(10%),它可能会正常工作。如果没有,请继续阅读。

如果您在视图树面板中注意到,它会显示嵌套在 3 个 FrameLayout 内的 LinearLayout。我的猜测是,最深的 FrameLayout 的宽度设置为 wrap_content,因此它不会填充父布局,并且仅与您的 LinearLayout 一样大。不过,我不能确定,因为您的图片中的尺寸被切掉了。

为什么当你删除标题时它会改变?我不知道。您可以通过调整 onMeasure 中的填充/布局参数来破解它,但似乎应该有一种更清洁的方法来做到这一点。

【讨论】:

  • 感谢您的回答,但 android:windowMinWidthMajor 和对话框 min_width 仍然没有帮助。
  • 嗯。您是否有理由不像 brillenheini 建议的那样使用Builder?这是我所知道的最简单的制作无标题对话框的方法,而且它对我来说很好。
  • 因为我想创建一个完全自定义的对话框,所以我需要使用Dialog而不是AlertDialog。上面的进度对话框只是一个例子,我还有很多复杂的对话框。
【解决方案4】:

仍然不知道为什么删除标题会使Dialog 不水平居中但是当我设置min_width attr of LinearLayout = dialog minWidth 这个问题就消失了。

【讨论】:

    猜你喜欢
    • 2014-05-27
    • 2013-12-23
    • 2018-10-10
    • 2012-01-01
    • 1970-01-01
    • 2012-05-24
    • 2011-02-08
    • 1970-01-01
    • 2018-03-10
    相关资源
    最近更新 更多