【问题标题】:Horizontal bounding of view in ConstraintLayoutConstraintLayout 中视图的水平边界
【发布时间】:2019-03-15 03:36:55
【问题描述】:

我有一个布局,其中 TextView(可变宽度)需要能够在父级的完整边界上水平移动(基于数据和视图事件)。

为了实现这一目标,我所做的就是使用带有指南的 ConstraintLayout。该指南有一个 app:layout_constraintGuide_percent 约束,我以编程方式在 0 和 1 之间进行调整,并且 TextView 被 layout_constraintEnd_toEndOflayout_constraintStart_toStartOf 约束到该指南。

这里的问题是,当该准则的 percent 接近 0 或 1(在我的情况下分别约为 0.05 和 0.95)时,TextView 开始消失在视图边界之外。

在 iOS 中,我会创建一个更高优先级的约束,将 TextView 限制在父级的范围内,但使用 Android ConstraintLayout 似乎不太可能?

完整的布局文件:

<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"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:paddingTop="20dp"
    >
  <TextView
      android:id="@+id/textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingBottom="5dp"
      android:visibility="visible"
      app:layout_constraintEnd_toEndOf="@+id/guideline"
      app:layout_constraintStart_toStartOf="@+id/guideline"
      app:layout_constraintTop_toTopOf="parent"
      tools:text="Variable text!"
      />

  <ImageView
      android:layout_width="match_parent"
      android:layout_height="300dp"
      app:layout_constraintTop_toBottomOf="@id/textview"
      />
  <androidx.constraintlayout.widget.Guideline
      android:id="@+id/guideline"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.5"
      />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 文本到达末尾会发生什么?对于TextView 来说,两侧是否应该是一个硬停止,这样即使指南靠近边缘,它也不会前进?

标签: android android-constraintlayout


【解决方案1】:

您可以使用 2 条垂直指南并将您的 textView 约束到它们,这样当 1 条指南移动时,另一条也将移动,您的 textView 将保持在您的屏幕范围内。

这是一个示例(请随意将指南移至 0.95 和 0.05):

<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:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.MenusDesign.ExpandableCategoriesMenu.ExpandableCategoriesMenu"
tools:layout_editor_absoluteY="81dp">

<TextView
    android:id="@+id/textview"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:paddingBottom="5dp"
    android:visibility="visible"
    app:layout_constraintEnd_toStartOf="@+id/guideline2"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Variable text!" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_marginTop="4dp"
    app:layout_constraintTop_toBottomOf="@id/textview"
    tools:layout_editor_absoluteX="0dp" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.0" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.18" />

您可以做的另一件事是使用this library 确保您的文本会根据剩余空间自行调整大小:

如果你想使用这个库将它添加到你的 gradle:

  implementation 'me.grantland:autofittextview:0.2.+'

现在只需创建您的 textView:

<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>

【讨论】:

    猜你喜欢
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2020-01-22
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多