繁体   English   中英

Android 2视图的简单布局

[英]Android simple layout of 2 views

无法理解如何实现简单的事情。 我需要具有以下行为的2个视图的布局:

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

当文本较长时,它将变为椭圆形,并且按钮始终可见且为全角

现在我知道该按钮不在屏幕上

您可以使用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>

初始设置为:

  • 创建一个包含文本和按钮的水平链
  • 将链样式设置为“压缩”,以便视图之间没有空间
  • 将水平偏差设置为0,以使打包视图向左拥抱

神奇之处在于TextView的宽度和app:layout_constraintWidth_default属性。 通过将宽度设置为0dp并包装“默认宽度”,我们告诉Android为视图提供尽可能多的空间来容纳其内容,只要它适合约束即可 当文本确实很长时,约束将阻止其将按钮从屏幕右侧推下。

在此处输入图片说明

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM