1. 程式人生 > >一個控制元件前面的自適應,後面的緊跟其後

一個控制元件前面的自適應,後面的緊跟其後

使用ConstraintLayout佈局

先在build.gradle裡面填加:

compile 'com.android.support.constraint:constraint-layout:1.1.2'

佈局檔案如下:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/red"
        android:singleLine="true"
        app:layout_constrainedWidth="true"
        android:text="我是前面的內容"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintBaseline_toBaselineOf="@+id/tv_right"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/tv_right" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是後面的內容"
        app:layout_constraintLeft_toRightOf="@id/tv_left"
        app:layout_constraintRight_toRightOf="parent" />


</android.support.constraint.ConstraintLayout>