Как предотвратить изменение размера TextView родительского LinearLayout - Android

У меня есть LinearLayout, который покрывает всю ширину экрана, внутри у меня есть случайное число TextView (7 в этом примере). Я хочу, чтобы все TextView были одинакового размера, поэтому я поместил атрибут android:layout_weight="1" в каждый.

Когда в TextView нет текста, все работает нормально:

введите здесь описание изображения

но когда я добавляю текст, соответствующий каждому TextView, размер Textview изменяется, не сохраняя вес и изменяя размер LinearLayout

введите здесь описание изображения

Это только часть дизайна моей активности, поэтому я воссоздал ее в новом xml, чтобы увидеть, сохраняется ли ошибка. И я заметил, что это происходит только тогда, когда LinearLayout находится внутри HorizontalScrollView.

Когда это не так, я получаю ожидаемый результат:

введите здесь описание изображения

Мой xml:

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">

        <TextView
            android:singleLine="true"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

    </LinearLayout>
</HorizontalScrollView>

Кто-нибудь знает, как это исправить? заранее спасибо.


person Oscar Méndez    schedule 02.01.2015    source источник
comment
установите высоту и ширину для textView динамически.   -  person Tushar Pandey    schedule 02.01.2015
comment
установите свойство размера текста в вашем TextView.   -  person chain    schedule 02.01.2015
comment
поскольку вы не хотите прокручивать свое представление, почему вы помещаете свое представление внутри horizontalScrollView?   -  person Shayan Pourvatan    schedule 02.01.2015
comment
Хорошо, подумайте о планшете, расположенном горизонтально. Как я уже упоминал, у меня есть случайное количество TextView, и я хочу, чтобы все они имели одинаковый размер. Однако, когда я переключаюсь в портретный режим, каждый TextView будет слишком маленьким, поэтому я добавляю минимальную ширину и HorizontalScrollView.   -  person Oscar Méndez    schedule 02.01.2015


Ответы (3)


попробуйте это, я пробовал с макетом таблицы

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <TableLayout
        android:id="@+id/table_dashboared"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchColumns="1" >

        <TableRow
            android:id="@+id/tablerow_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="7" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textSize="12sp"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />
        </TableRow>
    </TableLayout>

</HorizontalScrollView>
person M S Gadag    schedule 02.01.2015
comment
Я пробую это и отлично работаю, но как я могу установить minWidth для каждого TextView? Я не могу заставить это работать - person Oscar Méndez; 02.01.2015
comment
Вы хотите установить минимальную ширину? - person M S Gadag; 02.01.2015
comment
Подумайте о планшете, расположенном горизонтально, размер каждого TextView выглядит нормально. Однако, когда я переключаюсь в портретный режим, каждый TextView будет слишком маленьким, поэтому я добавляю минимальную ширину и прокручиваю содержимое. - person Oscar Méndez; 02.01.2015
comment
попробуйте с android:minEms="2" ...ширина должна быть обернута содержимым - person M S Gadag; 02.01.2015

Я немного изменил ваш xml. Попробуйте это и дайте мне знать результат

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="7">

    <TextView
        android:singleLine="true"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
       android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
       android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

</LinearLayout>

person Praveena    schedule 02.01.2015
comment
Добавить android:weightSum= не имеет значения, но спасибо за помощь - person Oscar Méndez; 02.01.2015
comment
Ширина всего текстового представления должна быть fill_parent - person Praveena; 02.01.2015
comment
То же самое, без разницы - person Oscar Méndez; 02.01.2015
comment
они имеют одинаковый размер, потому что они имеют одинаковый размер, попробуйте установить другой текст. - person Oscar Méndez; 02.01.2015

Попробуйте этот код:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="7" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXTTTTTTTTTTTTTTTTTTTTTTTTTT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />
</LinearLayout>
</HorizontalScrollView>

введите здесь описание изображения

person Iman Marashi    schedule 03.01.2015