Android简单布局2个视图

Dmi*_*try 1 android android-layout

无法理解如何实现简单的事情.我需要使用下一个行为布局2个视图:

文本短时,按钮应位于文本右侧

当文本很长时,它会椭圆化,按钮始终可见并且全宽

现在我得到那个按钮就会离开屏幕

Ben*_* P. 6

你可以使用这个来实现ConstraintLayout.这是一个模板:

<?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="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello world"
        android:maxLines="1"
        android:ellipsize="end"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HELLO WORLD"
        app:layout_constraintLeft_toRightOf="@+id/text"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBaseline_toBaselineOf="@+id/text"/>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

初始设置是:

  • 创建包含文本和按钮的水平链
  • 将链样式设置为"packed",以便视图之间没有空格
  • 将水平偏移设置为0,以便打包视图向左拥抱

魔术带有TextView宽度和app:layout_constraintWidth_default属性.通过将宽度设置为0dp并将"默认宽度"设置为包装,我们告诉Android为视图提供尽可能多的空间,只要它符合约束即可保存其内容.当文本非常长时,约束将使其不会按下屏幕右侧的按钮.

在此输入图像描述

在此输入图像描述