नहीं, एक्सएमएल फाइलों में कोड आधारित चर का उपयोग करना संभव नहीं है। हालांकि, आप इसके लिए शैलियों का उपयोग कर सकते हैं।
उदाहरण:
<style name="MyTvStyle">
<item name="android:textSize">40sp</item>
</style>
तो यह इस तरह लागू होते हैं:
<TextView style="@style/MyTvStyle" ... />
एक कोड आधारित दृष्टिकोण भी संभव है। यदि टेक्स्ट व्यूज़ में android:id
विशेषता परिभाषित है तो आप उन्हें findViewById
विधि के साथ कोड में पुनर्प्राप्त कर सकते हैं।
उदाहरण:
int[] id_array = {
R.id.textview1, R.id.textview2, R.id.textview3 //Put all the id's of your textviews here
}
for(int i : id_array) { //Loop trough all the id's and retrieve the Textview associated with it.
TextView textview = (TextView)findViewById(i);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,40); //Set the text size to 40sp
}
और हाँ यह हमेशा बेहतर सामान्य पिक्सेल मान के स्थान पर sp
उपयोग करने के लिए है। चूंकि sp
डिवाइस आकार और उपयोगकर्ता सेटिंग्स के साथ स्केल करेगा।
स्रोत
2012-09-02 14:08:40
धन्यवाद। मुझे शैलियों के बारे में भी पता नहीं था। – tomofv