Open WildOrangutan opened 1 year ago
wrap_content In text_view means the size of the text wins. Why are you not using 0dp ?
If I use 0dp
, text_view
will fill all the available space, which I don't want.
I want view_1
and text_view
to be packed together and centered (1st image), but be also be able to expand like in 2nd image. The only undesired effect is that parent layout expands for some reason (grey area).
Try this:
android:layout_width="0dp"
and app:layout_constraintWidth_max="wrap"
<TextView
android:id="@+id/text_view"
android:layout_width="0dp"
android:layout_height="20dp"
android:background="@android:color/holo_purple"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/white"
app:layout_constraintWidth_max="wrap"
app:layout_constraintEnd_toEndOf="@id/view_2"
app:layout_constraintStart_toEndOf="@id/view_1"
app:layout_constraintTop_toTopOf="parent"
tools:text="@tools:sample/lorem/random" />
It should give you want you want
Yeah, that has the desired effect.
I'm confused tough, shouldn't my initial code also work? At least I gathered from documentation, that this should be possible.
This has to do with priorities - who wins the "fight between wrap and constraints" in various scenarios.
Both satisfy the constraints
Constraints do not want to be negative.
width=wrap + constrainedWidth=true - wrap wins if at all possible
width=0dp + default=wrap - wrap wins after all other constraints are satisfied
It is hard to explain and ends up being super confusing in the documentation unless you encounter your type of problem. Adding it to the documentation would be a long confusing section to explain a corner case. We will update the documentation with a line: (Consider using width=0dp & default=wrap to adjust the priorities)
Some percentage of people approach the problem from with=0dp and never think about the subtlety. Some start with width=wrap encounter the issue try width=0dp, default=wrap Some like you get stuck on it.
Issue
When
layout_constrainedWidth
is set totrue
, it can expand parent layout.Use case
I'm trying to achieve following layout. I want![image](https://user-images.githubusercontent.com/5972966/189306756-7cb54fcb-39c1-4945-ada3-65dccfe2edbf.png)
text_view
to expand until it's limitied by size ofview_2
.But when![image](https://user-images.githubusercontent.com/5972966/189308529-70ce3bdc-4ef5-40f3-a126-b4a16c70501b.png)
layout_constrainedWidth
is set, it expands whole parent:Versions
androidx.constraintlayout:constraintlayout:2.1.4