2017-04-26 23 views
9

Tôi thực hiện một số ứng dụng đơn giản để thử nghiệm bằng cách sử dụng ConstraintLayout. Nhưng tôi có một số vấn đề.Android thêm bố cục khác trong ConstraintLayout sử dụng thẻ bao gồm

Đây là mã của tôi

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> 

kết quả Mã

Problem

Tôi muốn "content_main" là thuộc "thế giới Hellow!" TextView.

Tôi sử dụng RelativeLayout, LinearLayout, ConstraintLayout tại phần tử "content_main". nhưng không hoạt động.

Tôi tìm thấy bất kỳ giải pháp nào. Nhưng tôi chỉ thấy rằng làm thế nào để sử dụng ConstraintLayout.

Thẻ Android "include" không hoạt động tại ConstraintLayout?

Trả lời

13

Android bao gồm thẻ không làm việc với ConstraintLayout nhưng bạn cần phải khai báo lớn như thế nào là cách bố trí bạn muốn bao gồm với các thuộc tính sau đây

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

Sau đó, khó khăn của bạn nên làm việc.

+4

Sau đó, việc sử dụng bao gồm bố cục là gì nếu trong tương lai tôi sẽ phải thay đổi kích thước ở mọi nơi khác mà tôi đã bao gồm nó, mọi công việc xung quanh? Tôi bao gồm một dòng seperator kích thước 0,3 dp trên tất cả các ứng dụng của tôi và tôi muốn xác định chiều cao của nó tại chỉ một nơi – Ritzor

+2

Thực hành được khuyến nghị không sử dụng các giá trị mã hóa cứng như trên. Thay vào đó, bạn tạo một file dimens.xml trong res/values ​​của dự án Android của bạn, nơi bạn sẽ thêm nói 100dp ... và sau đó sử dụng nó trong xml ... android_layout_width = "@ dimen/include_layout_width" – taurelas

+1

'match_parent' cũng hoạt động –

Các vấn đề liên quan