I can´t understand constraints behaviour. Take a look to this layout
<?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"
tools:context=".MainActivity">
<Button
android:id="@ id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/purple_200"
android:text="whatever"
app:layout_constraintDimensionRatio="w, 1:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
It is supposed 0dp mean all available space. This button has both dimensions 0dp and a 1:1 ratio. And it is aligned left and top with parent so I guess it will be showing a square with a parent width as a side. But it is showing something unexpected for me, just a point on the upper left corner (even it is having some text):
It is clear I am not understanding this layout properly. Please enlighten me
CodePudding user response:
You need to add to more constraints in order to work
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
If you are using 0dp
for height, you need to constraint both top
and bottom
and vice versa
CodePudding user response:
Copy and past this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@ id/btn"
android:layout_width="126dp"
android:layout_height="53dp"
android:text="Button"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
i think you need to add more Constraint!
add this :
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
for this case, it will must like this :
<Button
android:id="@ id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/purple_200"
android:text="whatever"
app:layout_constraintDimensionRatio="w, 1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
hope this help you, have a great day! don't forget to take coffe time! Stay Coding and Stay Awesome!