将视图推到水平约束外部

时间:2019-05-21 09:56:29

标签: java android android-constraintlayout

如何在不使用Textview的情况下将长Textview和图标水平放置在视图之外。

玩过constrained_width等,但并不高兴。

<android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"">

                    <TextView
                        android:id="@+id/name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constrainedWidth="true"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        tools:text="Text in the middle pushes the edit icon out of view oh noh!" />

                    <ImageButton
                        android:id="@+id/edit"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/edit_icon"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toEndOf="@id/name"
                        app:layout_constraintTop_toTopOf="parent" />

                </android.support.constraint.ConstraintLayout>

我希望Textview和图标在中心对齐(水平)。但是随着文本的增长,Textview将编辑图标推到视图之外。我们如何将Textview(可变宽度)与其他任何物体水平放置,而又没有Textview将其他物体推到屏幕之外。

图标超出了范围

Icon pushed outside bounds

需要像这样,视图占据整个宽度 Need it to be something like this, with the views occupying the whole width

2 个答案:

答案 0 :(得分:1)

您需要chainStyle并需要修改视图的约束,像这样工作

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

    <TextView
        android:id="@+id/name"
        tools:text="Text in the middle pushes icon out of view oh noh! But we Resolved issue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        android:maxWidth="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/edit"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@color/colorPrimary"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/name" />

</android.support.constraint.ConstraintLayout>

enter image description here

答案 1 :(得分:0)

基本上,如果您的TextView同时具有指向父级的开始和结束约束,它将被完全拉伸。然后将ImageButton的开始约束设置为TextView的结束约束,将把ImageButton从布局中移出。

您应该做的是设置TextView,以便通过相应地设置左/右约束来使其适合父级和ImageBotton

app:layout_constraintEnd_toStartOf="@+id/edit"
app:layout_constraintStart_toStartOf="parent"

示例:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/edit"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Text in the middle pushes the edit icon out of view oh noh!"/>

    <ImageButton
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/edit_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here