9

शामिल है, मैं 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 दुनिया!" व्याख्यान दर्शन।

मैं RelativeLayout, LinearLayout, ConstraintLayout का उपयोग कर "content_main" तत्व पर। लेकिन काम नहीं करते हैं।

मुझे कोई समाधान मिल गया है। लेकिन मुझे लगता है कि ConstraintLayout का उपयोग कैसे करें।

एंड्रॉइड "शामिल" टैग ConstraintLayout पर काम नहीं करता है?

उत्तर

13

एंड्रॉयड टैग ConstraintLayout साथ काम करता है शामिल हैं, लेकिन आप घोषित करने के लिए कितना बड़ा लेआउट आप निम्नलिखित के साथ शामिल करना चाहते हैं की जरूरत विशेषताओं

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

उसके बाद, अपनी कमी काम करना चाहिए।

+4

फिर भविष्य में मुझे लेआउट को शामिल करने का क्या उपयोग है यदि मुझे इसे शामिल किया गया है, जहां मैं इसे शामिल करता हूं, किसी भी काम के आसपास आयाम बदलना होगा? मैं अपने ऐप पर आकार 0.3 डीपी की एक सेपरेटर लाइन भी शामिल कर रहा हूं और मैं अपनी ऊंचाई को केवल एक ही स्थान पर परिभाषित करना चाहता हूं – Ritzor

+2

अनुशंसित अभ्यास उपरोक्त के रूप में हार्डकोडेड मानों का उपयोग नहीं करना है। बल्कि, आप रेस में एक dimens.xml फ़ाइल बनाने/जहां जोड़ना होगा अपने Android परियोजना के मूल्यों कहना 100dp ... और उसके बाद एक्सएमएल में इसका इस्तेमाल करते हैं ... android_layout_width = "@ dimen/include_layout_width" – taurelas

+1

'match_parent' भी काम करता है –

संबंधित मुद्दे