DialogFragment 太宽,无法居中
DialogFragment too wide and can't center it
我已经尝试 google 一段时间了,none 的答案似乎对我有用。我在新片段中有 AlertDialog,无论我做什么,它都不会自行居中。我觉得我缺少一些基本的东西,比如以错误的方式膨胀视图。
这是现在的样子:
AlertDialog not centered
我希望视图居中而不是左侧。去掉多余的白色 space 也不错,但不是必需的,只要按钮居中即可。
我的代码如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
>
<!--Row 1. (colors 1-4)-->
<LinearLayout...>
<!--Row 2. (colors 5-8)-->
<LinearLayout...>
<!--Row 3. (colors 9-12)-->
<LinearLayout...>
</LinearLayout>
和
public class dialog_ThemePicker extends DialogFragment implements View.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_themepicker, null);
...
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle(R.string.dialog_theme_title);
alertDialog.setView(view);
alertDialog.show();
return alertDialog;
}
我试图将整个 XML 包装到 RelativeLayout
中,与 LayoutParams 和 Whosebug 的多个其他解决方案混淆。我错过了什么?
尝试将 match_parent 设置为您的 rootLayout 宽度,然后将 center
选项设置为重力
应该是这样的。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical">
<!--Row 1. (colors 1-4)-->
<LinearLayout...>
<!--Row 2. (colors 5-8)-->
<LinearLayout...>
<!--Row 3. (colors 9-12)-->
<LinearLayout...>
</LinearLayout>
你必须设置 android:gravity="center"
和 android:layout_width="match_parent"
在你的线性布局根元素中将根元素内的所有元素居中
我已经尝试 google 一段时间了,none 的答案似乎对我有用。我在新片段中有 AlertDialog,无论我做什么,它都不会自行居中。我觉得我缺少一些基本的东西,比如以错误的方式膨胀视图。
这是现在的样子: AlertDialog not centered
我希望视图居中而不是左侧。去掉多余的白色 space 也不错,但不是必需的,只要按钮居中即可。
我的代码如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
>
<!--Row 1. (colors 1-4)-->
<LinearLayout...>
<!--Row 2. (colors 5-8)-->
<LinearLayout...>
<!--Row 3. (colors 9-12)-->
<LinearLayout...>
</LinearLayout>
和
public class dialog_ThemePicker extends DialogFragment implements View.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_themepicker, null);
...
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle(R.string.dialog_theme_title);
alertDialog.setView(view);
alertDialog.show();
return alertDialog;
}
我试图将整个 XML 包装到 RelativeLayout
中,与 LayoutParams 和 Whosebug 的多个其他解决方案混淆。我错过了什么?
尝试将 match_parent 设置为您的 rootLayout 宽度,然后将 center
选项设置为重力
应该是这样的。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical">
<!--Row 1. (colors 1-4)-->
<LinearLayout...>
<!--Row 2. (colors 5-8)-->
<LinearLayout...>
<!--Row 3. (colors 9-12)-->
<LinearLayout...>
</LinearLayout>
你必须设置 android:gravity="center"
和 android:layout_width="match_parent"
在你的线性布局根元素中将根元素内的所有元素居中