Подтвердить что ты не робот

HorizontalScrollView с центрированными элементами

У меня есть HorizontalScrollView, который динамически заполняется TextViews. Когда-то есть один, два, три или более элемента. То, что я ищу, - это способ центрирования элементов независимо от количества элементов.

Например, с одним элементом:

| --------- MenuA --------- |

С двумя элементами:

| ---- MenuA --- MenuB ---- |

С тремя элементами:

| - MenuA - MenuB - MenuC - |

С пятью элементами (MenuD и MenuE скрыты):

| MenuA - MenuB - MenuC - Me|

Обновление: вот простой макет с двумя меню. Как настроить CenterA и MenuB?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <HorizontalScrollView android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/linearLayout1"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:orientation="horizontal">
            <TextView android:text="MenuA" android:id="@+id/textView1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            <TextView android:text="MenuB" android:id="@+id/textView2"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>
4b9b3361

Ответ 1

используйте атрибут тяжести:

Android: layout_gravity = "center_horizontal" EDIT: 03.30:

Я нашел его! только нужно было установить силу тяжести на верхний LinearLayout: все, что вам нужно сделать, это добавить немного дополнений/полей, чтобы сделать текстовые комментарии более удобными для чтения!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuA" >
            </TextView>

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

Держи меня в курсе!