问题
I was using this layout_constraintWidth_default="wrap" in textview to keep the textview content wrapped(to right of it there is imageview), as the text in textview increases textview area will grow which moves the imageview to right.
layout_constraintWidth_default="wrap"
As it is deprecated any alternatives?
Logcat
回答1:
As the documentation says in this link from Android Developers:
In versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension. While in general, this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attributes:
app:layout_constrainedWidth="true|false"
Hence, use layout_constrainedWidth="true"
instead of layout_constraintWidth_default="wrap"
.
回答2:
Following are the changes i have made for solution, imageview will not go outside the visible screen area :
android:layout_width="wrap_content"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text asdasdasdasdasdasdasdasdasdadsasdasdasdasdasdasdasd"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/image"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
回答3:
If a dimension is set to WRAP_CONTENT
, in versions before 1.1 they will be treated as a literal dimension-meaning, constraints will not limit the resulting dimension.
It’s often required that view width or height to remain as wrap content instead of match constraint or match parent but unfortunately wrap content override the constraint applied and overlap with the constraint if width or height changes. With version 1.1.0 this issue is resolved by using
app:layout_constrainedWidth="true" OR app:layout_constrainedHeight="true"
FYI
You can use percentage
for width and height the dimension should be match constraint(0dp)
and app:layout_constraintWidth_default="percent"
or app:layout_constraintHeight_default="percent"
need to set as percent.
Example
<TextView
android:id="@+id/txtView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello Width In Percentage"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintLeft_toLeftOf="parent" />
GRADLE
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' // For androidx
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
来源:https://stackoverflow.com/questions/56565195/layout-constraintwidth-default-wrap-is-deprecated-any-alternative