【问题标题】:How to position below the lowest of two views in ConstraintLayout?如何定位在 ConstraintLayout 中两个视图中最低的一个下方?
【发布时间】:2017-02-21 01:10:08
【问题描述】:

我有两个标题视图,HeaderViewAHeaderViewB。这些视图可以具有可见性visiblegone 的任意组合。

我需要将BigView 定位在低于HeaderViewA/HeaderViewB最低

如果不嵌套在ConstraintLayout 中,这可能吗?

【问题讨论】:

  • 我很想听到一个不同的答案,但这是不可能的。
  • 很有趣,但仅仅通过 XML 看起来是不可能的。您不想要嵌套视图,但如果您对编程操作持开放态度,则可以使用ConstraintSet
  • 为什么嵌套 ConstraintLayout 会破坏交易?
  • @AlexMeuer 嵌套布局会对性能产生负面影响(请参阅此报告 Understanding the performance benefits of ConstraintLayout)。所以这是保持布局尽可能平坦的主要目标,并且 ConstraintLayout 具有所有必要的功能。
  • @EugeneBrusov 所以我发现了。我相信我的评论是在屏障功能发布之前。 Here 对类似问题的看法略有不同。

标签: android android-layout android-support-library android-constraintlayout


【解决方案1】:

现在可以使用在约束布局 v1.1.0 中引入的 Barrier 类。

以下是针对您的特定情况的解决方案:

<?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.example.eugene.test10.MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#EEEEEE"
        android:text="TEXT_VIEW_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/textView2"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#DDDDDD"
        android:text="TEXT_VIEW_2"
        android:visibility="gone"
        app:layout_constraintLeft_toRightOf="@+id/textView1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Barrier
        android:id="@+id/labelBarrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textView1,textView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#CCCC00"
        android:text="TEXT_VIEW_3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/labelBarrier" />

</android.support.constraint.ConstraintLayout>

以下是使用此布局的结果:

您可以在 Codelab https://codelabs.developers.google.com/codelabs/constraint-layout/#10 上参考此分步指南。

【讨论】:

  • 在我的项目中我需要先做些什么才能实现障碍?
  • @Zerato 您需要将 'com.android.support.constraint:constraint-layout:1.1.0-beta3' 依赖项添加到模块的 gradle 文件中,并将 maven 存储库添加到项目的 gradle 文件中。你可以在这个 GitHub 项目github.com/googlecodelabs/constraint-layout 中找到所有依赖项
  • 对于那些希望通过 GUI 工具访问这些障碍的人,无法通过 textview、imageview 等进行搜索,而是在带有引导线的 I 形图标下拉菜单中进行搜索。跨度>
  • 它的androidx.constraintlayout.widget.Barrier 用于androidx
  • app:i_love_these="android_x,constraint_layout" />
【解决方案2】:
<?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">

    <TextView
        android:id="@+id/header_view_a"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_orange_light"
        android:gravity="center"
        android:text="HeaderViewA "
        android:layout_marginBottom="@dimen/sixteenDP"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/header_view_b"
        app:layout_constraintEnd_toStartOf="@+id/header_view_b"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintWidth_default="wrap" />

    <TextView
        android:id="@+id/header_view_b"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_light"
        android:gravity="center"
        android:text="HeaderViewB"
        android:textAlignment="center"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/guideline"
        app:layout_constraintStart_toEndOf="@+id/header_view_a"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.4"/>

    <TextView
        android:id="@+id/big_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:background="@android:color/holo_blue_dark"
        android:gravity="center"
        android:text="BigView"
        android:textAlignment="center"
        android:textSize="@dimen/list_item_height"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        app:layout_constraintVertical_bias="1.0"/>

</android.support.constraint.ConstraintLayout>

Watch out 用于app:layout_constraintWidth_default="wrap"宽度设置为 0dp)。如果设置,小部件将具有与使用 wrap_content 相同的大小,但会受到约束(即它不会超出它们)和app:layout_constraintVertical_bias="1.0" 的限制。使用偏置将 BigView 拉到其父级的底部。

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多