ConstraintLayout एक ऐसा लेआउट है जिसकी मदद से, स्क्रीन पर मौजूद अन्य कंपोज़ेबल के हिसाब से कंपोज़ेबल को रखा जा सकता है. यह कई नेस्ट किए गए Row, Column, Box, और अन्य कस्टम लेआउट एलिमेंट इस्तेमाल करने का विकल्प है.
View system में, बड़े और जटिल लेआउट बनाने के लिए ConstraintLayout का इस्तेमाल करने का सुझाव दिया जाता था. ऐसा इसलिए, क्योंकि फ़्लैट व्यू हैरारकी, नेस्ट किए गए व्यू की तुलना में परफ़ॉर्मेंस के लिए बेहतर होती है. हालांकि, Compose में ऐसा नहीं है. यह डीप लेआउट हैरारकी को आसानी से मैनेज कर सकता है. इसलिए, ConstraintLayout उतना फ़ायदेमंद नहीं है.
ConstraintLayout का कॉन्टेंट देखना शुरू करें
Compose में ConstraintLayout का इस्तेमाल करने के लिए, आपको build.gradle में यह डिपेंडेंसी जोड़नी होगी. इसके अलावा, आपको Compose सेटअप भी करना होगा:
implementation "androidx.constraintlayout:constraintlayout-compose:$constraintlayout_compose_version"
ConstraintLayout, Compose में डीएसएल का इस्तेमाल करके इस तरह काम करता है:
createRefs()याcreateRefFor()का इस्तेमाल करके,ConstraintLayoutमें मौजूद हर कंपोज़ेबल के लिए रेफ़रंस बनाएं.constrainAs()मॉडिफ़ायर का इस्तेमाल करके, कंस्ट्रेंट दिए जाते हैं. यह रेफ़रंस को पैरामीटर के तौर पर लेता है और आपको बॉडी लैम्डा में इसके कंस्ट्रेंट तय करने की अनुमति देता है.linkTo()या अन्य मददगार तरीकों का इस्तेमाल करके, पाबंदियां तय की जाती हैं.parentएक मौजूदा रेफ़रंस है. इसका इस्तेमाल,ConstraintLayoutकंपोज़ेबल पर लागू होने वाली पाबंदियों के बारे में बताने के लिए किया जा सकता है.
यहां ConstraintLayout का इस्तेमाल करके बनाए गए कंपोज़ेबल का एक उदाहरण दिया गया है:
@Composable fun ConstraintLayoutContent() { ConstraintLayout { // Create references for the composables to constrain val (button, text) = createRefs() Button( onClick = { /* Do something */ }, // Assign reference "button" to the Button composable // and constrain it to the top of the ConstraintLayout modifier = Modifier.constrainAs(button) { top.linkTo(parent.top, margin = 16.dp) } ) { Text("Button") } // Assign reference "text" to the Text composable // and constrain it to the bottom of the Button composable Text( "Text", Modifier.constrainAs(text) { top.linkTo(button.bottom, margin = 16.dp) } ) } }
इस कोड में, Button के टॉप को पैरंट के साथ 16.dp मार्जिन पर अलाइन किया गया है. साथ ही, Text को Button के बॉटम के साथ 16.dp मार्जिन पर अलाइन किया गया है.
Button और Text, दोनों को ConstraintLayout में एक-दूसरे के साथ कंपोज़ किया गया है.
Decoupled API
ConstraintLayout उदाहरण में, कंस्ट्रेंट को इनलाइन तौर पर तय किया गया है. साथ ही, कंपोज़ेबल में मॉडिफ़ायर का इस्तेमाल किया गया है. हालांकि, कुछ मामलों में, पाबंदियों को उन लेआउट से अलग करना बेहतर होता है जिन पर वे लागू होती हैं.
उदाहरण के लिए, स्क्रीन कॉन्फ़िगरेशन के आधार पर, आपको कंस्ट्रेंट बदलने या दो कंस्ट्रेंट सेट के बीच ऐनिमेशन बनाने की ज़रूरत पड़ सकती है.
इस तरह के मामलों के लिए, ConstraintLayout का इस्तेमाल अलग तरीके से किया जा सकता है:
ConstraintLayoutको पैरामीटर के तौर परConstraintSetपास करें.layoutIdमॉडिफ़ायर का इस्तेमाल करके,ConstraintSetमें बनाए गए रेफ़रंस को कंपोज़ेबल असाइन करें.
@Composable fun DecoupledConstraintLayout() { BoxWithConstraints { val constraints = if (minWidth < 600.dp) { decoupledConstraints(margin = 16.dp) // Portrait constraints } else { decoupledConstraints(margin = 32.dp) // Landscape constraints } ConstraintLayout(constraints) { Button( onClick = { /* Do something */ }, modifier = Modifier.layoutId("button") ) { Text("Button") } Text("Text", Modifier.layoutId("text")) } } } private fun decoupledConstraints(margin: Dp): ConstraintSet { return ConstraintSet { val button = createRefFor("button") val text = createRefFor("text") constrain(button) { top.linkTo(parent.top, margin = margin) } constrain(text) { top.linkTo(button.bottom, margin) } } }
इसके बाद, जब आपको शर्तों में बदलाव करना हो, तो सिर्फ़ एक अलग ConstraintSet पास करें.
ConstraintLayout कॉन्सेप्ट
ConstraintLayout में दिशा-निर्देश, रुकावटें, और चेन जैसे कॉन्सेप्ट शामिल हैं. ये आपके कंपोज़ेबल में एलिमेंट को सही जगह पर रखने में मदद कर सकते हैं.
दिशा-निर्देश
दिशा-निर्देश, लेआउट डिज़ाइन करने में मदद करने वाले छोटे विज़ुअल होते हैं. कंपोज़ेबल को किसी गाइडलाइन के हिसाब से सीमित किया जा सकता है. गाइडलाइन, पैरंट कंपोज़ेबल के अंदर किसी dp या percentage पर एलिमेंट को पोज़िशन करने के लिए काम आती हैं.
दिशा-निर्देश दो तरह के होते हैं: वर्टिकल और हॉरिज़ॉन्टल. हॉरिज़ॉन्टल वाले दो डाइमेंशन top और bottom हैं. वहीं, वर्टिकल वाले दो डाइमेंशन start और end हैं.
ConstraintLayout { // Create guideline from the start of the parent at 10% the width of the Composable val startGuideline = createGuidelineFromStart(0.1f) // Create guideline from the end of the parent at 10% the width of the Composable val endGuideline = createGuidelineFromEnd(0.1f) // Create guideline from 16 dp from the top of the parent val topGuideline = createGuidelineFromTop(16.dp) // Create guideline from 16 dp from the bottom of the parent val bottomGuideline = createGuidelineFromBottom(16.dp) }
दिशा-निर्देश बनाने के लिए, createGuidelineFrom* का इस्तेमाल करें. साथ ही, दिशा-निर्देश का टाइप भी तय करें. इससे एक रेफ़रंस बनता है, जिसका इस्तेमाल Modifier.constrainAs() ब्लॉक में किया जा सकता है.
बैरियर
Barriers, कई कंपोज़ेबल का रेफ़रंस देता है, ताकि तय की गई साइड पर मौजूद सबसे बड़े विजेट के आधार पर वर्चुअल गाइडलाइन बनाई जा सके.
बैरियर बनाने के लिए, createTopBarrier() (या: createBottomBarrier(),
createEndBarrier(), createStartBarrier()) का इस्तेमाल करें. साथ ही, उन रेफ़रंस के बारे में बताएँ जिनसे बैरियर बनाना है.
ConstraintLayout { val constraintSet = ConstraintSet { val button = createRefFor("button") val text = createRefFor("text") val topBarrier = createTopBarrier(button, text) } }
इसके बाद, बैरियर का इस्तेमाल Modifier.constrainAs() ब्लॉक में किया जा सकता है.
चेन
चेन, एक ही ऐक्सिस (हॉरिजॉन्टल या वर्टिकल) में ग्रुप की तरह काम करती हैं. दूसरे ऐक्सिस को अलग से सीमित किया जा सकता है.
चेन बनाने के लिए, createVerticalChain या createHorizontalChain का इस्तेमाल करें:
ConstraintLayout { val constraintSet = ConstraintSet { val button = createRefFor("button") val text = createRefFor("text") val verticalChain = createVerticalChain(button, text, chainStyle = ChainStyle.Spread) val horizontalChain = createHorizontalChain(button, text) } }
इसके बाद, चेन का इस्तेमाल Modifier.constrainAs() ब्लॉक में किया जा सकता है.
किसी चेन को अलग-अलग ChainStyles के साथ कॉन्फ़िगर किया जा सकता है. इससे यह तय होता है कि कंपोज़ेबल के आस-पास की जगह को कैसे मैनेज किया जाए. जैसे:
ChainStyle.Spread: स्पेस को सभी कंपोज़ेबल के बीच बराबर बांटा जाता है. इसमें पहले कंपोज़ेबल से पहले और आखिरी कंपोज़ेबल के बाद का खाली स्पेस भी शामिल है.ChainStyle.SpreadInside: स्पेस को सभी कंपोज़ेबल के बीच बराबर बांटा जाता है. पहले कंपोज़ेबल से पहले या आखिरी कंपोज़ेबल के बाद कोई खाली जगह नहीं होती.ChainStyle.Packed: स्पेस को पहले और आखिरी कंपोज़ेबल के बीच में बांटा जाता है. कंपोज़ेबल को एक-दूसरे के बीच बिना किसी स्पेस के एक साथ रखा जाता है.