繁体   English   中英

如何防止TextView调整父级LinearLayout的大小-Android

[英]How to prevent a TextView resize the parent LinearLayout - Android

我有一个LinearLayout可以覆盖屏幕的整个宽度,里面有一个随机数的TextView (在此示例中为7)。 我希望所有TextView的大小均相同,因此我将android:layout_weight="1"放入其中。

当TextView中没有文本时,一切正常:

在此处输入图片说明

但是当我添加与每个TextView对应的文本时, Textview的大小Textview更改,而不是保持权重并调整LinearLayout大小

在此处输入图片说明

这只是我的Activity设计的一部分,因此我在新的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>

有人知道怎么修这个东西吗? 提前致谢。

试试这个我试过表布局

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

我对您的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>

试试这个代码:

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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