【问题标题】:Why my TextView invisible with long text in previous TextView in LinearLayout?为什么我的 TextView 在 LinearLayout 中的先前 TextView 中以长文本不可见?
【发布时间】:2019-05-07 14:50:33
【问题描述】:

我有这个 XML:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView            
            style="@style/Icon"
            tools:background="@color/Black54Opacity" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="24dp"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <TextView                    
                    android:layout_width="wrap_content"
                    android:layout_height="24dp"                                        
                    android:ellipsize="end"                    
                    android:includeFontPadding="false"                    
                    android:maxLines="1"
                    android:padding="0dp"
                    android:textColor="@android:color/black"
                    android:textSize="20sp"
                    tools:text="LONG LONG LONG" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="18dp"                    
                    android:minWidth="18dp"
                    tools:text="2342"
                    tools:visibility="visible" />
            </LinearLayout>

            <TextView                
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:gravity="center_vertical|end"
                android:textColor="4CAF50"
                tools:text="EDIT" />
        </LinearLayout>
    </LinearLayout>

当我在我的第一个 TextView 中插入一个短文本时 - 我没有问题。

当我在第一个 textView 中插入长文本时 - 我的第二个 TextView 移动到了第三个 TextView 下。

我希望第一个 TextView 中的文本可以调整到那个点,这样其他两个 TextView 就不会改变它们的大小。

第一个TextView中的长文本最后被椭圆化了。 第二个 TextView 必须始终压在第一个 TextView 的左侧。

【问题讨论】:

  • 你能给我看看你的 XML 布局屏幕吗?
  • @Niceumang 我用图片更新了我的问题。
  • @Sergey 尝试为您的第二个 TextView 添加权重
  • 重量对我没有帮助,因为我不能更改第二个 TextView 的宽度。第二个 TextView 必须绑定到第一个 TextView 的右侧。

标签: android xml position android-linearlayout


【解决方案1】:

我们通过ConstraintLayout 找到了解决方案。

  1. 我们删除所有LinearLayout
  2. 然后我们在我的第一个 TextView 中添加了app:layout_constrainedWidth="true"。这是一个非常重要的属性。

【讨论】:

    【解决方案2】:

    为您的 TextView 使用权重

    试试这个

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    
    <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@mipmap/ic_launcher"
     android:fitsSystemWindows="true"
     android:scaleType="fitXY"
     android:id="@+id/img"/>
    
    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:weightSum="5"
     android:orientation="horizontal">
    
    
    <TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"/>
    
    <TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2" />
    
    <TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:layout_weight="1"
    android:text="Edit"/>
    
    </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 它不起作用。当我使用权重时,我在第二个 TextView 中的文本没有绑定到第一个 TextView 的左侧。截图:joxi.ru/GrqyyNMC4zjJxr
    • 请分享你的xml
    • 你也可以去掉第二个TextView的权重,设置wrap的宽高
    • 没用。我的第二个 TextView 没有再次绑定到第一个 TextView 的右侧...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多