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
940 views
in Technique[技术] by (71.8m points)

android - TextView dynamic width with a view at the end

I have two elements, a TextView and an ImageView. The ImageView will be at the end of the TextView. The TextView's width will be change based on the text, and the ImageView also follows the end of the TextView. Like this:

enter image description here

But the problem is, if the text is so long, the ImageView goes out of the screen, like this:

enter image description here

But it should be like this:

enter image description here

How can I get this only by XML? I know programatically it can be achieved, but I want a XML solution.

Note: The ImageView can be a Button, so I can't use drawableEnd like solution.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

you can try this the key here is app:layout_constrainedWidth="true"

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="lefttttttttttttttttttttttttttttttttttttttttttttttttt"
    app:layout_constrainedWidth="true"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@id/right"
    app:layout_constraintTop_toTopOf="parent"/>
 <ImageView
    android:id="@+id/right"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@mipmap/ic_launcher"
    app:layout_constraintStart_toEndOf="@id/left"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@id/left"
    />
</android.support.constraint.ConstraintLayout>

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

...