【问题标题】:LinearLayout TextView weight breaks with long textLinearLayout TextView 权重因长文本而中断
【发布时间】:2017-10-15 10:30:34
【问题描述】:

当我在线性布局中向我的TextView 添加长文本时,它会占用所有空间,并且其他视图会被挤压。我将layout_width="0" 设置为每个视图,但没有帮助。我还应该补充一点,这个布局用于RecyclerView

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100">

<TextView
    android:textSize="12sp"
    android:gravity="center"
    android:id="@+id/lp"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="Lp"
    android:layout_weight="5" />

//this TextView takes all the space if text is long, but it behaves normally if text is short
<TextView
    android:textSize="12sp"
    android:gravity="center"
    android:id="@+id/nazwa"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="Końcówka Yankauer do odsysania pola operacyjnego CH 23 (4 otwory boczne) z kontrolą odsysania" 
    android:layout_weight="20"/>

<TextView
    android:textSize="12sp"
    android:gravity="center"
    android:id="@+id/ilosc"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="#"
    android:layout_weight="5" />

<TextView
    android:textSize="12sp"
    android:gravity="center"
    android:id="@+id/jednostka"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="JM"
    android:layout_weight="15" />

<TextView
    android:textSize="12sp"
    android:gravity="center"
    android:id="@+id/cenaNetto"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="C. net."
    android:layout_weight="15" />

<TextView
    android:textSize="12sp"
    android:gravity="center"
    android:id="@+id/wartNetto"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="W.net."
    android:layout_weight="15" />

<TextView
    android:textSize="12sp"
    android:gravity="center"
    android:id="@+id/wartVAT"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="VAT"
    android:layout_weight="10" />

<TextView
    android:textSize="12sp"
    android:gravity="center"
    android:id="@+id/wartBrutto"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="W. brut."
    android:layout_weight="15" />

</android.support.v7.widget.LinearLayoutCompat>

这是我的应用程序出现问题的打印屏幕link

【问题讨论】:

  • 为什么要对所有 textView 使用高度“match_parent”?
  • 因为如果我不这样做,它看起来像this
  • 如果你想提供百分比虎钳视图而不是尝试新概念的 android PercentRelativeLayout link
  • @ashish 我决定采用您的解决方案,它有助于解决我的问题,但现在我的观点看起来像 this。有谁知道如何让所有的 textViews 和最大的一样高?

标签: android xml android-layout android-linearlayout


【解决方案1】:

请尝试下面的链接代码作为演示,我希望你能得到你的结果。

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="#ffffff"
             android:shrinkColumns="*"
             android:stretchColumns="*">
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
             <TextView
                android:id="@+id/row_my_booking_unit_name"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center">
            </TextView>
             <TextView
                android:id="@+id/row_my_booking_unit_quantity"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center">
            </TextView>
             <TextView
                android:id="@+id/row_my_booking_unit_amount"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center">
            </TextView>
        </LinearLayout>
    </TableRow>
</TableLayout>

【讨论】:

  • 谢谢,这就是我一直在寻找的,现在一切似乎都正常了。
【解决方案2】:

如果您希望每个 textview 占用相同的空间,那么您可以为 all 分配 layout_weight=1。

【讨论】:

  • 但我不希望每个视图占用相同的空间。我希望每个视图都占据一定百分比的空间并且它可以工作,但是如果我在其中一个文本视图中设置太长的文本,它会中断。
【解决方案3】:

您在TextView 中尝试过吗?

android:ellipsize="end" 
android:maxLines="1"

【讨论】:

  • 是的,但是布局看起来还是一样的,只是可见的文字更少了。
  • @musztardus 是你的RecyclerView 水平吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2012-01-26
  • 1970-01-01
相关资源
最近更新 更多