如何在Android ConstraintLayout中为视图设置最大宽度?

43

我有一个布局看起来像这样:

<?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"
    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" />

我想将seekBar默认设置为一定的宽度,但如果屏幕不够宽,它会填满整个屏幕(带边距)。我尝试过设置layout_width,但如果太宽,它就会超出屏幕。使用max_width则完全没有任何效果。在ConstraintLayout中是否有可能实现我的要求?如果不行,应该使用什么?


你应该为不同的屏幕分辨率使用不同的布局。 - Giacomo Lai
ContraintLayout旨在最小化需要为不同屏幕分辨率拥有不同布局的需求,因为它应该根据设置的约束自适应屏幕。 因此得名。 - the_new_mr
2个回答

121
我认为你要找的是 matchConstraintMaxWidth 属性。

如果你将其添加到你的 SeekBar 中,并设置一个值,比如 200dp,保持左右约束与父控件相同且宽度为 0dp,它将把视图的宽度拉伸到 200dp,但如果有更多的空间,它将保持在 200dp。

所以你的xml应该看起来像这样:

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

您还应该能够以编程方式设置它

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

那是ConstraintLayout.LayoutParams类中的一个字段吗?它必须在运行时设置吗? - clarkep
正确,它是ConstraintLayout.LayoutParams的一个字段,我想它也可以通过Java设置。 - lelloman
我认为这是一个不好的评论,因为所有屏幕都需要更改值。 - Morozov
如果你的意思是这种做法不好,一般来说我同意。然而,LayoutParams总是在运行时被显式地实例化和设置,可以在你的代码中直接声明或由LayoutInflater执行。在大多数情况下,将所有内容在xml中声明更方便,但也可能有些情况需要在Java或Kotlin中实例化或设置一些值,哈哈。 - lelloman
3
要使其起作用,您需要将android:layout_width="0dp"设置为此项。 - Bolein95

3
请注意,如果您的视图只有一个水平约束,则需要添加此行app:layout_constraintWidth_default="percent"
<SeekBar
   ...
    android:layout_width="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintWidth_default="percent"
    app:layout_constraintWidth_max="200dp" />

这在你想要实现layout_gravity=start效果的情况下很有帮助。 - yaugenka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接