【问题标题】:Android add other layout in ConstraintLayout using include tagAndroid使用include标签在ConstraintLayout中添加其他布局
【发布时间】:2017-09-23 08:27:08
【问题描述】:

我使用 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”位于“Hellow world!”之下文本视图。

我在“content_main”元素中使用RelativeLayout、LinearLayout、ConstraintLayout。但不起作用。

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

Android“包含”标签在 ConstraintLayout 中不起作用吗?

【问题讨论】:

    标签: android xml android-layout layout android-constraintlayout


    【解决方案1】:

    Android include 标记确实适用于 ConstraintLayout,但您需要使用以下属性声明要包含的布局有多大。

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

    对于具有动态高度的包含布局,使用 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"/>
    

    之后,您的约束应该会起作用。

    【讨论】:

    • 那么,如果将来我必须在包含它的每个其他地方更改尺寸,那么包含布局有什么用,有什么解决方法吗?我在我的应用程序中包含一条大小为 0.3 dp 的分隔线,我只想在一个地方定义它的高度
    • 推荐的做法是不要使用上面的硬编码值。相反,您在 Android 项目的 res/values 中创建一个 dimens.xml 文件,在其中添加say 100dp ... 然后在xml ... android_layout_width="@dimen/included_layout_width"
    • wrap_content 和 match_parent 也可以。您只需要指定宽度和高度。它们不必只是数字。
    • 使用这种方法,包含在设计视图中仍然不可见,需要手动添加约束
    • @kkarakk 尝试在原始布局中使用 tools:showIn 属性,使其在包含它的其他布局中可见。 developer.android.com/studio/write/…
    【解决方案2】:

    在约束布局中定义高度和宽度以及您的约束如下:

    <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"/>
    

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

    干杯!

    【讨论】:

      【解决方案3】:

      不确定是否是理想的解决方案,但对我有用的是让 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 的文本视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-08
        • 1970-01-01
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多