Android使用include标记在ConstraintLayout中添加其他布局

Blu*_*ght 35 xml layout android android-layout android-constraintlayout

我使用ConstraintLayout制作一些简单的应用程序进行测试.但我有一些问题.

这是我的代码

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.myapplication.activity.MainActivity">

    <Button
        android:id="@+id/btn_launch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="16dp"
        android:text="launch"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="16dp"
        android:text="Hello World!"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_launch" />

    <include
        layout="@layout/content_main"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_view" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="123456"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="98765"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="abc"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

代码结果

问题

我希望" content_main "在"Hellow world"之下!TextView中.

我在" content_main "元素中使用RelativeLayout,LinearLayout,ConstraintLayout .但不行.

我找到任何解决方案.但我只是发现如何使用ConstraintLayout.

Android"include"标记在ConstraintLayout中不起作用吗?

tau*_*las 80

Android include标签可以使用ConstraintLayout,但您需要使用以下属性声明要包含的布局有多大.

<include
     layout="@layout/content_main"
     android:layout_width="100dp"
     android:layout_height="250dp"
     .../>
Run Code Online (Sandbox Code Playgroud)

对于包含动态高度的布局,使用包含标记wrap_content的值layout_heightlayout_width属性.

<include
    android:id="@+id/input_include"
    layout="@layout/task_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

在那之后,你的约束应该工作.

  • 那么包含布局的用途是什么,如果将来我将不得不在我包含它的每个其他地方改变尺寸,任何解决方法?我在我的应用程序中包括一个大小为0.3 dp的分隔线,我想在一个地方定义它的高度 (8认同)
  • wrap_content和match_parent也有效.你只需要指定宽度和高度.他们不一定只是数字. (4认同)
  • 建议的做法是不要使用上面的硬编码值.相反,您在Android项目的res/values中创建一个dimens.xml文件,您可以在其中添加say <resources> <dimen name ="included_layout_width"> 100dp </ dimen> ... </ resources>,然后将其用于xml ... android_layout_width ="@ dimen/included_layout_width" (2认同)

Der*_*ked 5

在约束布局中定义高度和宽度以及约束,如下所示:

<include
    app:layout_constraintTop_toBottomOf="@id/custom_members_toolbar_layout"
    app:layout_constraintBottom_toTopOf="@id/file_xfer_bottom_nav_bar"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    layout="@layout/file_xfer_upload_layout"/>
Run Code Online (Sandbox Code Playgroud)

这将根据您的限制自动调整高度。

干杯!