How to set maximum width for presentation in android constraintLayout?

I have a layout that looks like this:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayoutxmlns: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="com.plarke.proto1.QuizActivity"> <Button android:id="@+id/compare_settings" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp" android:layout_marginRight="16dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginEnd="16dp" /> <TextView android:id="@+id/compare_score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp" android:layout_marginLeft="16dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginStart="16dp" /> <SeekBar android:id="@+id/compare_pitchbar" android:layout_width="0dp" android:layout_height="23dp" android:layout_marginBottom="50dp" android:layout_marginEnd="16dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginStart="16dp" android:progressBackgroundTint="@color/colorAccent" android:progressBackgroundTintMode="src_over" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="50dp" app:layout_constraintBottom_toTopOf="@+id/button2" tools:text="The seekbar is all the way to the left." app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginRight="0dp" android:layout_marginLeft="0dp" /> 

And I want to change it so that seekBar has a certain width by default, but if the screen is not wide enough, it just fills the screen (with margins). I tried setting layout_width, but then it just leaves the screen if it is too wide. Using max_width does nothing. Is this possible in ConstrainstLayout? If not, what should I use?

+5
source share
1 answer

I think you are looking for matchConstraintMaxWidth .

If you add it to a SeekBar with a value of, say, 200dp , while preserving the left and right restrictions for the parent and the width of 0dp , it will stretch the view width to 200 dp, but if there is more space, it will remain 200 dp.

so your xml looks like this:

 <SeekBar ... android:layout_width="0dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintWidth_max="200dp" /> 

You can also install it programmatically using

 ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, 0); layoutParams.matchConstraintMaxWidth = ... 
+20
source

Source: https://habr.com/ru/post/1269678/


All Articles