我总是使用 styles.xml
中的 android:windowNoTitle
或 requestWindowFeature(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;
}
这是它在屏幕上的显示方式
如果我删除 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/