Jetpack Compose, आपके Android प्रोजेक्ट में तय किए गए संसाधनों को ऐक्सेस कर सकता है. इस दस्तावेज़ में, Compose के उन एपीआई के बारे में बताया गया है जिनकी मदद से ऐसा किया जा सकता है.
संसाधन, वे अतिरिक्त फ़ाइलें और स्टैटिक कॉन्टेंट होते हैं जिनका इस्तेमाल आपका कोड करता है. जैसे, बिटमैप, लेआउट की परिभाषाएं, यूज़र इंटरफ़ेस स्ट्रिंग, ऐनिमेशन के निर्देश वगैरह. अगर आपको Android में संसाधनों के बारे में जानकारी नहीं है, तो ऐप्लिकेशन के संसाधनों की खास जानकारी देने वाला लेख पढ़ें.
स्ट्रिंग
संसाधन के तौर पर, सबसे ज़्यादा स्ट्रिंग का इस्तेमाल किया जाता है. अपने एक्सएमएल संसाधनों में स्टैटिक तौर पर तय की गई स्ट्रिंग को वापस पाने के लिए, stringResource एपीआई का इस्तेमाल करें.
// In the res/values/strings.xml file // <string name="compose">Jetpack Compose</string> // In your Compose code Text( text = stringResource(R.string.compose) )
stringResource, पोज़िशनल फ़ॉर्मैटिंग के साथ भी काम करता है.
// In the res/values/strings.xml file // <string name="congratulate">Happy %1$s %2$d</string> // In your Compose code Text( text = stringResource(R.string.congratulate, "New Year", 2021) )
प्लूरल स्ट्रिंग (आज़माने के लिए)
किसी खास संख्या के लिए प्लूरल लोड करने के लिए, pluralStringResource एपीआई का इस्तेमाल करें.
// In the res/strings.xml file // <plurals name="runtime_format"> // <item quantity="one">%1$d minute</item> // <item quantity="other">%1$d minutes</item> // </plurals> // In your Compose code Text( text = pluralStringResource( R.plurals.runtime_format, quantity, quantity ) )
अगर आपकी स्ट्रिंग में, संख्या के साथ स्ट्रिंग फ़ॉर्मैटिंग शामिल है, तो pluralStringResource तरीके का इस्तेमाल करते समय, आपको संख्या को दो बार पास करना होगा. उदाहरण के लिए, स्ट्रिंग %1$d minutes के लिए, पहला काउंट पैरामीटर सही प्लूरल
स्ट्रिंग चुनता है और दूसरा काउंट पैरामीटर %1$d प्लेसहोल्डर में डाला जाता है.
अगर आपकी प्लूरल स्ट्रिंग में स्ट्रिंग फ़ॉर्मैटिंग शामिल नहीं है, तो आपको pluralStringResource में तीसरा पैरामीटर पास करने की ज़रूरत नहीं है.
प्लूरल के बारे में ज़्यादा जानने के लिए, संख्या वाली स्ट्रिंग का दस्तावेज़ देखें.
डाइमेंशन
इसी तरह, संसाधन वाली एक्सएमएल फ़ाइल से डाइमेंशन पाने के लिए, dimensionResource एपीआई का इस्तेमाल करें.
// In the res/values/dimens.xml file // <dimen name="padding_small">8dp</dimen> // In your Compose code val smallPadding = dimensionResource(R.dimen.padding_small) Text( text = "...", modifier = Modifier.padding(smallPadding) )
रंग
अगर अपने ऐप्लिकेशन में Compose को धीरे-धीरे शामिल किया जा रहा है, तो संसाधन वाली एक्सएमएल फ़ाइल से रंग पाने के लिए, colorResource एपीआई का इस्तेमाल करें.
// In the res/colors.xml file // <color name="purple_200">#FFBB86FC</color> // In your Compose code HorizontalDivider(color = colorResource(R.color.purple_200))
colorResource स्टैटिक रंगों के साथ उम्मीद के मुताबिक काम करता है. हालांकि, यह कलर
स्टेट लिस्ट संसाधनों को फ़्लैट कर देता है.
वेक्टर ऐसेट और इमेज संसाधन
वेक्टर ड्रॉएबल या रास्टराइज़्ड ऐसेट फ़ॉर्मैट (जैसे, PNG) लोड करने के लिए, painterResource एपीआई का इस्तेमाल करें. आपको ड्रॉएबल का टाइप जानने की ज़रूरत नहीं है. बस
painterResource का इस्तेमाल Image कंपोज़ेबल या paint मॉडिफ़ायर में करें.
// Files in res/drawable folders. For example: // - res/drawable-nodpi/ic_logo.xml // - res/drawable-xxhdpi/ic_logo.png // In your Compose code Icon( painter = painterResource(id = R.drawable.ic_logo), contentDescription = null // decorative element )
painterResource , मुख्य थ्रेड पर संसाधन के कॉन्टेंट को डिकोड और पार्स करता है.
ऐनिमेटेड वेक्टर ड्रॉएबल
ऐनिमेटेड वेक्टर ड्रॉएबल एक्सएमएल लोड करने के लिए, AnimatedImageVector.animatedVectorResource एपीआई का इस्तेमाल करें. यह तरीका, AnimatedImageVector इंस्टेंस दिखाता है. ऐनिमेटेड इमेज दिखाने के लिए, तरीके का इस्तेमाल करके rememberAnimatedVectorPainter बनाएँ. इसका इस्तेमाल Painter और Image कंपोज़ेबल में किया जा सकता है.Icon
rememberAnimatedVectorPainter तरीके का बूलियन atEnd पैरामीटर, यह बताता है कि सभी ऐनिमेशन के खत्म होने पर इमेज को ड्रॉ किया जाना चाहिए या नहीं.
अगर इसका इस्तेमाल, बदलाव किए जा सकने वाली स्थिति के साथ किया जाता है, तो इस वैल्यू में होने वाले बदलाव से जुड़ा ऐनिमेशन ट्रिगर होता है.
// Files in res/drawable folders. For example: // - res/drawable/ic_hourglass_animated.xml // In your Compose code val image = AnimatedImageVector.animatedVectorResource(R.drawable.ic_hourglass_animated) val atEnd by remember { mutableStateOf(false) } Icon( painter = rememberAnimatedVectorPainter(image, atEnd), contentDescription = null // decorative element )
आइकॉन
Jetpack Compose में Icons ऑब्जेक्ट शामिल है. यह Compose में
मटीरियल आइकॉन इस्तेमाल करने का एंट्री पॉइंट है.
आइकॉन की पांच अलग-अलग थीम हैं:
फ़िल,
आउटलाइन,
राउंडेड,
टू टोन, और
शार्प. हर थीम में एक ही आइकॉन होते हैं, लेकिन उनका विज़ुअल स्टाइल अलग होता है. आम तौर पर, आपको एक थीम चुननी चाहिए और अपने ऐप्लिकेशन में एक जैसी थीम का इस्तेमाल करना चाहिए.
किसी आइकॉन को ड्रॉ करने के लिए, Icon
कंपोज़ेबल का इस्तेमाल किया जा सकता है. यह आइकॉन पर टिंट लागू करता है और लेआउट का साइज़ आइकॉन के साइज़ के मुताबिक सेट करता है.
Icon(Icons.Rounded.Menu, contentDescription = "Localized description")
सबसे ज़्यादा इस्तेमाल किए जाने वाले कुछ आइकॉन, androidx.compose.material डिपेंडेंसी के तौर पर उपलब्ध हैं. अन्य मटीरियल आइकॉन का इस्तेमाल करने के लिए, build.gradle फ़ाइल में material-icons-extended डिपेंडेंसी जोड़ें.
dependencies {
def composeBom = platform('androidx.compose:compose-bom:2026.03.00')
implementation composeBom
implementation 'androidx.compose.material:material-icons-extended'
}
फ़ॉन्ट
Compose में फ़ॉन्ट इस्तेमाल करने के लिए, फ़ॉन्ट फ़ाइलें डाउनलोड करें और उन्हें सीधे अपने APK में बंडल करें. इसके लिए, उन्हें res/font फ़ोल्डर में रखें.
Font एपीआई का इस्तेमाल करके, हर फ़ॉन्ट लोड करें और उनसे
FontFamily बनाएं. इसका इस्तेमाल, अपनी
Typography बनाने के लिए
TextStyle इंस्टेंस में किया जा सकता है. यहां दिया गया कोड,
Crane
compose के सैंपल और उसकी
Typography.kt
फ़ाइल से लिया गया है.
// Define and load the fonts of the app private val light = Font(R.font.raleway_light, FontWeight.W300) private val regular = Font(R.font.raleway_regular, FontWeight.W400) private val medium = Font(R.font.raleway_medium, FontWeight.W500) private val semibold = Font(R.font.raleway_semibold, FontWeight.W600) // Create a font family to use in TextStyles private val craneFontFamily = FontFamily(light, regular, medium, semibold) // Use the font family to define a custom typography val craneTypography = Typography( titleLarge = TextStyle( fontFamily = craneFontFamily ) /* ... */ ) // Pass the typography to a MaterialTheme that will create a theme using // that typography in the part of the UI hierarchy where this theme is used @Composable fun CraneTheme(content: @Composable () -> Unit) { MaterialTheme(typography = craneTypography) { content() } }
Compose में डाउनलोड किए जा सकने वाले फ़ॉन्ट इस्तेमाल करने के लिए, डाउनलोड किए जा सकने वाले फ़ॉन्ट पेज देखें.
टाइपोग्राफ़ी के बारे में ज़्यादा जानने के लिए, Compose में थीमिंग का दस्तावेज़ देखें
आपके लिए सुझाव
- ध्यान दें: JavaScript बंद होने पर, लिंक का टेक्स्ट दिखता है
- इमेज लोड हो रही हैं {:#loading-images}
- Compose में मटीरियल डिज़ाइन 2
- Compose में पसंद के मुताबिक बनाए गए डिज़ाइन सिस्टम