为什么我的 TextView 在之前的 LinearLayout TextView 中看不到长文本?
Why my TextView invisible with long text in previous TextView in LinearLayout?
我有这个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 的左侧。
为您的 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>
我们通过ConstraintLayout
找到了解决方案。
- 我们删除所有
LinearLayout
。
- 然后我们在我的第一个 TextView 中添加了
app:layout_constrainedWidth="true"
。这是一个非常重要的属性。
我有这个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 的左侧。
为您的 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>
我们通过ConstraintLayout
找到了解决方案。
- 我们删除所有
LinearLayout
。 - 然后我们在我的第一个 TextView 中添加了
app:layout_constrainedWidth="true"
。这是一个非常重要的属性。