首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DialogFragment太宽,无法居中

DialogFragment太宽,无法居中
EN

Stack Overflow用户
提问于 2019-06-14 18:15:23
回答 2查看 202关注 0票数 1

我已经尝试用谷歌搜索了很长一段时间,但似乎没有一个答案对我有效。我在新的片段中有AlertDialog,无论我做什么,它都不会居中。我觉得我错过了一些基本的东西,比如以错误的方式放大视图。

下面是它现在的样子:AlertDialog not centered

我希望视图居中,而不是在左侧。去掉多余的空白也不错,但不是必须的,只要按钮居中即可。

下面是我的代码:

代码语言:javascript
复制
<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>

代码语言:javascript
复制
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的其他多种解决方案。我遗漏了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-14 18:22:34

试着给你的重力宽度赋予match_parent,然后给重力赋予center选项,这是它应该是的。

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2019-06-14 18:24:28

您必须在线性布局根元素中设置android:gravity="center"android:layout_width="match_parent",以使根元素中的所有元素居中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56596083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档