2012-01-05 12 views
42

मैं अभी शुरुआत Android डेवलपर का उपयोग करना चाहिए, मैं ग्रहण में इस रैखिक लेआउट को चलाने के लिए कोशिश कर रहा था:हार्डकोडेड स्ट्रिंग "पंक्ति में तीन", @string संसाधन

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <TextView 
      android:text="red" 
      android:gravity="center_horizontal" 
      android:background="#aa0000" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="green" 
      android:gravity="center_horizontal" 
      android:background="#00aa00" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="blue" 
      android:gravity="center_horizontal" 
      android:background="#0000aa" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:text="yellow" 
      android:gravity="center_horizontal" 
      android:background="#aaaa00" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    <TextView 
     android:text="row one" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row two" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row three" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row four" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    </LinearLayout> 

</LinearLayout> 

और, मैंने देखा है:
1) android:text="Yellow"
2) पीले रंग की रेखा के नीचे पीले रंग की रेखा android:text="row four"
त्रिभुज चेतावनी दी है के तहत कहते हैं [I18N] Hardcoded string "Yellow", should use @string resource " और बाकी चेतावनियों के लिए भी। कोई सुझाव?

उत्तर

96

यह आपकी लेआउट फ़ाइलों में हार्ड कोड स्ट्रिंग के लिए अच्छा अभ्यास नहीं है। आपको उन्हें एक स्ट्रिंग संसाधन फ़ाइल में जोड़ना चाहिए और फिर उन्हें अपने लेआउट से संदर्भित करना चाहिए।

यह आपको अपने स्ट्रिंग्स.एक्सएमएल फ़ाइल को संपादित करके एक ही समय में सभी लेआउट में "पीला" शब्द की हर घटना को अपडेट करने की अनुमति देता है।

यह कई भाषाओं का समर्थन करने के लिए भी बेहद उपयोगी है क्योंकि अलग-अलग तारों के रूप में एक अलग स्ट्रिंग्स.एक्सएमएल फ़ाइल का उपयोग किया जा सकता है।

उदाहरण: एक्सएमएल res/values ​​/ strings.xml में सहेजी गई फ़ाइल:

<TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/yellow" /> 

इसी रंग रंग में संग्रहित किया जाना चाहिए:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="yellow">Yellow</string> 
</resources> 

यह लेआउट एक्सएमएल एक दृश्य के लिए एक स्ट्रिंग लागू होता है .xml और फिर @ रंग/color_name

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="Black">#000000</color> 
</resources> 
+3

(दायरे से बाहर) भी, यह किसी भी तरह की तालिका जैसा दिखता है। एक टेबललेआउट हो सकता है और अधिक उपयुक्त होगा। इसके अलावा, निराश में पीटी में textsize। एसपी अधिक लचीला हैं। – njzk2

+0

@kuffs: धन्यवाद, यह उपयोगी था –

+0

यह @string/... क्यों नहीं है @strings/...? –

10

का उपयोग करके संदर्भित किया गया है उन्हें strings.xml के तहत बनाने

<string name="close">Close</string>  

आप बदलना होगा और संदर्भ इस

android:text="@string/close"/> 

तरह @strings का उपयोग न करें, भले ही एक्सएमएल फ़ाइल का कहना है strings.xml वरना यह काम नहीं करेगा।

+2

महान समझाया – CroiOS

+4

@ क्रोओओएस ग्रेट इंग्लिश –

8

यह आपके लेआउट फ़ाइलों/कोड में हार्ड कोड स्ट्रिंग के लिए अच्छा अभ्यास नहीं है। आपको उन्हें एक स्ट्रिंग संसाधन फ़ाइल में जोड़ना चाहिए और फिर उन्हें अपने लेआउट से संदर्भित करना चाहिए।

  1. यह आपको सिर्फ अपने strings.xml फ़ाइल का संपादन करके एक ही समय में सब
    लेआउट में एक ही शब्द के हर घटना अद्यतन करने के लिए अनुमति देता है।
  2. यह भी प्रत्येक समर्थित भाषा
  3. @string प्रणाली होने कृपया localization प्रलेखन से अधिक पढ़ने के लिए की वास्तविक बिंदु के लिए इस्तेमाल किया जा सकता supporting multiple languages एक अलग strings.xml file के रूप में के लिए अत्यंत उपयोगी है। यह आपको में अपने ऐप को आसानी से टेक्स्ट ढूंढने की अनुमति देता है और बाद में इसका अनुवाद किया जाता है।
  4. स्ट्रिंग्स को आसानी से अंतर्राष्ट्रीयकृत किया जा सकता है, जिससे आपके आवेदन से support multiple languages with a single application package file (एपीके) को अनुमति मिल सकती है।

लाभ

  • कहते हैं कि तुम कोड में 10 अलग अलग स्थानों में एक ही स्ट्रिंग इस्तेमाल किया करते हैं। यदि आप इसे बदलने का निर्णय लेते हैं तो क्या होगा? प्रोजेक्ट में यह कहने के बजाय कि यह का उपयोग करने के बजाय आप इसे एक बार बदलते हैं और परियोजना में हर जगह परिलक्षित होते हैं।
  • स्ट्रिंग्स अपने एप्लिकेशन कोड को अव्यवस्थित नहीं करते हैं, इसे स्पष्ट करते हुए और बनाए रखने में आसान है।
+3

सर्वश्रेष्ठ उत्तर नीचे है .... – Blubber

0

आप डिज़ाइन मोड पर जा सकते हैं और चेतावनी के नीचे "ठीक करें" का चयन कर सकते हैं। फिर एक पॉप अप दिखाई देगा (ऐसा लगता है कि यह नई स्ट्रिंग को पंजीकृत करने जा रहा है) और वॉयला, त्रुटि ठीक हो गई है।

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