我已经尝试用谷歌搜索了很长一段时间,但似乎没有一个答案对我有效。我在新的片段中有AlertDialog,无论我做什么,它都不会居中。我觉得我错过了一些基本的东西,比如以错误的方式放大视图。
下面是它现在的样子:AlertDialog not centered
我希望视图居中,而不是在左侧。去掉多余的空白也不错,但不是必须的,只要按钮居中即可。
下面是我的代码:
<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和StackOverflow的其他多种解决方案。我遗漏了什么?
发布于 2019-06-14 18:22:34
试着给你的重力宽度赋予match_parent,然后给重力赋予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>发布于 2019-06-14 18:24:28
您必须在线性布局根元素中设置android:gravity="center"和android:layout_width="match_parent",以使根元素中的所有元素居中
https://stackoverflow.com/questions/56596083
复制相似问题