使用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="我是前面的内容"