使用include标签在ConstraintLayout中添加其他布局的Android方法

55
我是一个有用的助手,可以为您翻译文本。
我正在使用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>

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>

代码结果

问题

我想让“content_main”在“Hello world!”TextView下面。

我在“content_main”元素中使用了RelativeLayout、LinearLayout和ConstraintLayout,但它们都不起作用。

我找不到任何解决方案。

Android的“include”标签是否不能与ConstraintLayout一起使用?

3个回答

140

Android include标签能够与ConstraintLayout一起使用,但您需要使用以下属性来声明所需包含的布局的大小。

<include
     layout="@layout/content_main"
     android:layout_width="100dp"
     android:layout_height="250dp"
     .../>

如果包含的布局高度是动态的,可以在include标签的layout_heightlayout_width属性中使用wrap_content作为值。

<include
    android:id="@+id/input_include"
    layout="@layout/task_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

之后,你的约束条件应该会生效。


9
那么,如果以后我需要在包含它的其他地方更改尺寸,那么包含布局有什么用呢?有什么解决办法吗?我想在整个应用程序中都包含一个高度为0.3 dp的分隔线,并且我只想在一个地方定义其高度。 - Ritzor
3
推荐的做法是不要像上面那样使用硬编码的值。相反,你可以在你的 Android 项目的 res/values 目录下创建一个 dimens.xml 文件,在其中添加以下内容: <resources> <dimen name="included_layout_width">100dp</dimen> ... </resources>然后在xml中使用它: ... android_layout_width="@dimen/included_layout_width" - Maciej Beimcik
6
wrap_content和match_parent也可以使用,您只需要指定宽度和高度。它们不一定要是数字。 - Amir Ziarati
使用这种方法仍然无法在设计视图中显示include,需要手动添加约束。 - Karan Harsh Wardhan
1
@kkarakk,尝试在原始布局中使用tools:showIn属性,使其在包含它的其他布局中可见。https://developer.android.com/studio/write/tool-attributes.html#toolsshowin - Nikos Hidalgo

4
在约束布局中,定义高度和宽度以及以下约束条件:
<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"/>

这将根据您的约束自动调整高度。
干杯!

0

不确定是否是理想的解决方案,但对我有效的方法是将include标签托管到其自己的约束布局中,例如

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/todaysFightLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/tvLatestUpdate"
    app:layout_constraintStart_toStartOf="parent">

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

</androidx.constraintlayout.widget.ConstraintLayout>

然后从这个新的约束布局中添加约束到下一个要显示的项目,对于我的情况是具有id tvLatestUpdate的文本视图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接