简体   繁体   中英

How to prevent a TextView resize the parent LinearLayout - Android

I have a LinearLayout that covers the entire width of the screen, inside I have a random number of TextView (7 in this example). I want all the TextView are the same size, so I put the attribute android:layout_weight="1" in each.

When there is not text in the TextView everything works fine:

在此处输入图片说明

but when I add the text corresponding to each TextView , the size of Textview change, not keep the weight and resize the LinearLayout

在此处输入图片说明

This is only part of the design of my Activity, so I recreated it in a new xml to see if the error persists. And I have noticed that this only happens when the LinearLayout is inside of a HorizontalScrollView

When it is not, I get the expected result:

在此处输入图片说明

My 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>

Anyone know how to fix this? thanks in advance.

try this i tried with table layout

<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>

I have changed your xml a bit. Try this and let me know the result

<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>

Try this code:

<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>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM