2013-02-22 45 views
19

मैं प्रोग्रामेटिक रूप से एक टेक्स्ट व्यू बना रहा हूं। क्या कोई तरीका है कि मैं इस पाठ की शैली को सेट कर सकता हूं? कुछमैं प्रोग्राम की दृष्टि से टेक्स्ट की शैली कैसे सेट करूं?

style="@android:style/TextAppearance.DeviceDefault.Small" 

जो मैं लेआउट.एक्सएमएल फ़ाइल था, तो मैं इसका उपयोग करता हूं।

+0

आप किसी भी दृश्य की शैली को व्यावहारिक रूप से सेट नहीं कर सकते हैं। लेकिन आप दृश्य के व्यक्तिगत गुणों को सेट कर सकते हैं –

+0

[एंड्रॉइड - सेट टेक्स्टव्यू टेक्स्ट स्टाइल प्रोग्रामेटिक रूप से सेट करें] के संभावित डुप्लिकेट? (http://stackoverflow.com/questions/7919173/android-set-textview-textstyle-programmatically) –

उत्तर

34

आप प्रोग्राम की दृष्टि से शैली की शैली निर्धारित नहीं कर सकते हैं, लेकिन आप textView.setTextAppearance(context, android.R.style.TextAppearance_Small); जैसे कुछ करने में सक्षम हो सकते हैं।

+0

इस विधि को बहिष्कृत के रूप में चिह्नित किया गया है। – pkuszewski

+7

'if (Build.VERSION.SDK_INT <23) { textView.setTextAppearance (संदर्भ, android.R.style का उपयोग करें।TextAppearance_Small); } अन्य { textView.setTextAppearance (android.R.style.TextAppearance_Small); } ' – Nedko

27

वर्तमान में प्रोग्राम की दृष्टि से शैली को सेट करना संभव नहीं है।

इस के आसपास पाने के लिए आप, शैली सौंपा के साथ एक टेम्पलेट लेआउट एक्सएमएल फ़ाइल बनाने res/layout में उदाहरण के लिए निम्नलिखित सामग्री के साथ के रूप में tvtemplate.xml बना सकते हैं:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="This is a template" 
     style="@android:style/TextAppearance.DeviceDefault.Small" /> 

तो यह बढ़ अपने नए TextView का दृष्टांत के लिए:

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null); 
+1

मुझे यह समाधान पसंद है स्वीकार किए गए उत्तर से बेहतर है क्योंकि यह स्टाइल का समर्थन करता है क्योंकि ओपी ने अनुरोध किया था (और मैं चाहता था)। इसके अलावा, यह हमेशा ऐसा लगता है .... गलत .... एक android.widget कन्स्ट्रक्टर का उपयोग करने के लिए। –

1

रन टाइम (कोड में) आप निर्माता के लिए शैली प्रदान कर सकते हैं पर उत्पन्न विचार के लिए: View(Context, AttributeSet, int)

अन्य मामले आप कॉल करनी होगी में तरीकों varios बदलने के लिए उपस्थिति

4

इस

textview.setTextAppearance(context, R.style.yourstyle); 
प्रयास करें

यह काम नहीं कर सकता है इस

textviewstyle.xml 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     style="@android:style/TextAppearance.DeviceDefault.Small" /> 
जैसे टेक्स्टव्यू के साथ एक एक्सएमएल बनाना

वांछित शैली एक्सएमएल TextView

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvstyle, null); 
3

वास्तव में होता है, जो बढ़ पाने के लिए, इस एपीआई स्तर के रूप में संभव है 21.

TextView है एक 4 parameter constructor:

TextView (Context context, 
      AttributeSet attrs, 
      int defStyleAttr, 
      int defStyleRes) 

बीच दो इस मामले में पैरामीटर आवश्यक नहीं हैं। निम्नलिखित कोड एक गतिविधि में सीधे एक TextView बनाता है और केवल अपनी शैली संसाधन परिभाषित करता है:

TextView myStyledTextView = new TextView(this, null, 0, R.style.my_style); 
0
@Deprecated 
public void setTextAppearance(Context context, @StyleRes int resId) 

इस प्रक्रिया में सुरक्षित संस्करण का उपयोग कर सकते हैं एंड्रॉयड एसकेडी 23.

की नहीं रहेगी:

if (Build.VERSION.SDK_INT < 23) { 
    super.setTextAppearance(context, resId); 
} else { 
    super.setTextAppearance(resId); 
}