Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

android - Multiline text view not showing the full text

I have the following layout with a LinearLayout that contains a Material ShapableImageView and a TextView. I need my TextView to take 2 lines if it's width is bigger than the ImageView's with, but I can't figure out how to do it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/company_card_layout"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:foreground="?attr/selectableItemBackground"
  android:orientation="vertical"
  android:paddingStart="@dimen/padding_small"
  android:paddingEnd="@dimen/padding_small"
  android:paddingBottom="@dimen/padding_small">

  <com.google.android.material.imageview.ShapeableImageView
    android:id="@+id/category_logo"
    android:layout_width="120dp"
    android:layout_height="96dp"
    android:layout_gravity="center_horizontal"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_avatar_default_tinted"
    app:shapeAppearanceOverlay="@style/AppTheme.ShapeableImageView.Squircle" />

  <TextView
    android:id="@+id/category_name"
    style="@style/AppTheme.Text.Caption.Light.Darker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:maxLines="2"
    android:textStyle="bold"
    tools:text="This is a very long text and should take at least 2 lines" />

</LinearLayout>

This is how it's currently being shown and as you can see, it doesn't show the full text:

enter image description here

question from:https://stackoverflow.com/questions/65881459/multiline-text-view-not-showing-the-full-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seeing that you're limiting the width of your LinearLayout by the width of your image, you can set its width to the same, which should help. This allows the measure pass (I believe) to "better" define all the measure constraints/rules.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/company_card_layout"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/category_logo"
        android:layout_width="120dp"
        android:layout_height="96dp"
        android:layout_gravity="center_horizontal"
        android:scaleType="centerCrop"
        android:src="@drawable/my_drawable" />

    <TextView
        android:id="@+id/category_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="This is a very long text and should take at least 2 lines" />
</LinearLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...