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