【问题标题】:How to set a maximum width for a view in an android constraintLayout?如何在android constraintLayout中设置视图的最大宽度?
【发布时间】:2017-07-10 18:13:51
【问题描述】:

我的布局如下所示:

<?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 什么都不做。在 ConstrainstLayout 中我想要什么?如果不是我应该使用什么?

【问题讨论】:

  • 你应该为不同的屏幕分辨率使用不同的布局。
  • ContraintLayout 应该最大限度地减少对不同屏幕分辨率的不同布局的需求,因为它应该根据设置的约束适应屏幕。因此得名。

标签: android android-layout android-constraintlayout


【解决方案1】:

我认为您正在寻找的是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 类中的一个字段?必须在运行时设置吗?
  • 正确,它是ConstraintLayout.LayoutParams的一个字段,我想也可以通过Java设置
  • 我认为这是一个不好的评论,因为所有屏幕都需要更改值
  • 如果你的意思是这是不好的做法,一般来说,我同意。但是,LayoutParams 总是在运行时被实例化和设置,在你的代码中或由 LayoutInflater 显式设置。在大多数情况下,在 xml 中声明所有内容会更方便,但在某些情况下,您可能需要在 java 或 kotlin 中实例化或设置一些值:D
  • 你必须设置android:layout_width="0dp"才能工作
【解决方案2】:

请注意,如果您的视图只有一个水平约束,那么您需要添加这一行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 效果时很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 2018-08-21
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多