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

标签 android dialog

我总是使用 styles.xml 中的 android:windowNoTitlerequestWindowFeature(Window. FEATURE_NO_TITLE) 但我的一些对话框不是居中水平的,例如这个对话框:

<?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:

@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;
}

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

enter image description here

如果我删除 android:windowNoTitle 属性,这个对话框会正确显示,所以只有在使用没有标题的对话框时才会出现问题。

有谁知道为什么会发生这种情况以及如何使对话始终在屏幕上居中?

最佳答案

你试过看这个帖子吗?

How to align custom dialog centre in android ?

 android:layout_gravity="center" 

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

关于没有标题的Android对话框没有水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740093/

相关文章:

android - 抽屉导航问题(不显示布局预览)

以编程方式创建组件时的 Android layout_weight

javascript - InDesign ScriptUI 窗口出现后立即消失

android - if else 语句中的警报对话框生成器 - android

java - 如何在 ListView 项目的对话框上显示自定义消息

android - 使用 values-swdp 在 Android 中针对不同的屏幕尺寸

java - 如何在 Android 中通过指纹搜索

iphone - Flurry vs localytics?

android - 设置扩展 Dialog 的 CustomDialog 主题的最佳实践是什么 OnClickListener

android - 无法添加窗口—— token android.os.BinderProxy@42824 无效;你的 Activity 在运行吗?