Java 视图水平推送到约束布局外部
如何水平放置长Java 视图水平推送到约束布局外部,java,android,android-constraintlayout,Java,Android,Android Constraintlayout,如何水平放置长Textview和图标,而不让Textview将图标推到视图外 玩受限制的\u宽度等,但没有乐趣 <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content""> <Tex
Textview
和图标,而不让Textview
将图标推到视图外
玩受限制的\u宽度
等,但没有乐趣
<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
的ImageButton
开始约束到结束约束,将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>
您需要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>
我需要将textview居中对齐,因此约束与父对象对齐。我必须用硬编码的宽度(textview)来解决这个问题。谢谢你的帮助。好的,我基于你提供的布局