2017-04-26 45 views
9

Tworzę prostą aplikację do testowania przy użyciu ConstraintLayout. Ale mam pewien problem.Android dodać inny układ w ConstraintLayout przy użyciu znacznika include

Oto mój kod

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> 

wynik Kod

Problem

chcę "content_main" być pod "Hellow świecie!" Widok tekstu.

Używam RelativeLayout, LinearLayout, ConstraintLayout w elemencie "content_main". ale nie działa.

Znajduję jakieś rozwiązanie. Ale właśnie stwierdzam, że jak korzystać z ConstraintLayout.

Czy tag "include" Androida nie działa w ConstraintLayout?

Odpowiedz

13

Android obejmują tag działa z ConstraintLayout ale trzeba zadeklarować, jak duży jest układ chcesz dołączyć z następujących atrybutów

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

Po wykonaniu tej czynności ograniczenia powinno działać.

+4

Więc jaki jest pożytek z umieszczenia układu, jeśli w przyszłości będę musiał zmienić wymiary w każdym innym miejscu, w którym je zawarłem, jakiejkolwiek pracy? Do mojej aplikacji dołączam oddzielną linię o rozmiarze 0.3 dp i chcę zdefiniować jej wysokość tylko w jednym miejscu. – Ritzor

+2

Zalecaną praktyką nie jest stosowanie wartości zakodowanych jak powyżej. Przeciwnie, należy utworzyć plik dimens.xml w res/wartości projektu Android gdzie dodasz powiedzieć 100dp ... a następnie używać go w xml ... android_layout_width = "@ dimen/included_layout_width" – taurelas

+1

Działa również 'match_parent' –