0

我有一个ConstraintLayout如下:

<?xml version="1.0" encoding="utf-8"?>
<merge 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_margin="10dp"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">




    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.8" />

    <include layout="@layout/layout_btns" />

</merge>

为清楚起见,省略了指南之上的内容。
引用的布局layout_btns

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/guide"
    >

    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginEnd="4dp"
        android:text="Action 1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/btn2"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintTop_toBottomOf="@id/guide" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Action 2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@id/btn1"
        app:layout_constraintTop_toBottomOf="@id/guide" />

</LinearLayout>  

这有效,但我注意到以下内容:

  1. <include>布局中的 IDE 在 IDE 中显示警告:Element include is not allowed here如果我将其更改mergeConstraintLayout
  2. 如果我将约束移到声明之外,layout_btnsinclude约束不起作用。即使我添加到includewidth/height = match_parent/0dp 呈现的布局也是完全错误的

为什么会出现这些问题?

4

0 回答 0