2017-04-26 591 views
9

我使用ConstraintLayout制作一些简单的测试应用程序。但我有一些问题。Android使用include标签在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> 

码结果

Problem

我想 “content_main” 是在 “Hellow世界!” TextView中。

我使用RelativeLayout,LinearLayout,ConstraintLayout的“content_main”元素。但没有工作。

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

Android的“include”标签在ConstraintLayout中不工作吗?

回答

13

的Android 包括标签确实与ConstraintLayout工作,但需要声明有多大,你想用下面的包括布局属性

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

之后,你的约束应该工作。

+4

那么如果将来我需要改变尺寸,那么包括布局在内的所有其他地方都可以改变尺寸?我在我的应用程序中包括一个大小为0.3dp的分隔线,我只想在一个地方定义它的高度 – Ritzor

+2

建议的做法不是像上面那样使用硬编码值。相反,您创建一个资源文件dimens.xml /在那里你会增加你的Android项目的价值说 <扪名=“included_layout_width”> 100dp ... ,然后在XML 使用... android_layout_width =“@ dimen/included_layout_width” – taurelas

+1

'match_parent'也适用 –