DialogFragment слишком широкий и не может центрировать его - PullRequest
1 голос
/ 14 июня 2019

Я пытался гуглить довольно долгое время, и ни один из ответов, похоже, не работает для меня.У меня AlertDialog в новом фрагменте, и что бы я ни делал, он просто не будет центрироваться.Я чувствую, что упускаю что-то простое, например, неправильно надуваю представление.

Вот как это выглядит сейчас: AlertDialog не центрирован

Я хочувиды должны быть в центре, а не на левой стороне.Удаление лишних пробелов было бы неплохо, но не обязательно, если кнопки расположены по центру.

Вот как выглядит мой код:

<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.Чего мне не хватает?

Ответы [ 2 ]

0 голосов
/ 14 июня 2019

Вы должны установить android:gravity="center" и android:layout_width="match_parent" в вашем линейном макете корневой элемент для центрирования всех элементов внутри корневого элемента

0 голосов
/ 14 июня 2019

Попробуйте задать 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>
...