【问题标题】:ConstraintLayout and Placeholder do not work as expected with child MATCH_CONSTRAINTConstraintLayout 和 Placeholder 不能按预期与子 MATCH_CONSTRAINT 一起工作
【发布时间】:2021-04-07 12:52:00
【问题描述】:

我使用下面的 XML 来使用占位符。 但使用占位符时我只能看到button1'root_container3'

'root_container2' 不是占位符,因为 MATCH_CONSTRAINT

android:id="@+id/button2"
android:layout_width="0dp"  // MATCH_CONSTRAINT
android:layout_height="0dp" // MATCH_CONSTRAINT

我猜button2 不是用match_constraint 衡量的 (如果按钮具有特定的维度值,例如button3,它可以工作,但 Match_constraint 没有。)

我不知道这个..

<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/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.Placeholder
        android:id="@+id/place_holder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/root_container2"
        android:layout_width="200dp"
        android:layout_height="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="Button2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/root_container3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <Button
            android:id="@+id/button3"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:text="Button3"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

/

        button1.setOnClickListener {
            place_holder.setContentId(R.id.button1)
        }

        button2.setOnClickListener {
            place_holder.setContentId(R.id.root_container2)
        }

        button3.setOnClickListener {
            place_holder.setContentId(R.id.root_container3)
        }

【问题讨论】:

  • 您能否添加建议的屏幕设计以提高清晰度

标签: android android-constraintlayout placeholder


【解决方案1】:

你应该在 ConstraintLayout 中使用占位符

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/root_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#FF00FF"
  app:layout_constraintRight_toRightOf="parent"
  app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.Placeholder
    android:id="@+id/place_holder"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:backgroundTint="#DC2C2C"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.482"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/place_holder"
    app:layout_constraintVertical_bias="0.485" />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 感谢您的指导。抱歉,我跳过了整个 XML,但我已经在 ConstraintLayout 中使用了占位符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多