TextView dynamic width with a view at the end

后端 未结 2 1569
挽巷
挽巷 2020-12-21 14:58

I have two elements, a TextView and an ImageView. The ImageView will be at the end of the TextView. The TextView

相关标签:
2条回答
  • 2020-12-21 15:52

    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>
    
    0 讨论(0)
  • 2020-12-21 15:53

    You can change layoutDirection to fix the direction of layout, example

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:layoutDirection="rtl"
        android:gravity="left">
        <Button
            android:layout_width="100dp"
            android:layout_height="40dp"
            app:layout_constraintStart_toEndOf="@id/left"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/left"
            android:text="abc"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="How can I get this only by XML? I know programatically it can be achieved, but I want a XML solution."/>
    
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题