2012-06-22 11 views
6

में गतिशील रूप से देखने के लिए शैली को लागू करना मेरे पास कस्टम बटन दृश्य है।जावा कोड

public class PrayerTimeLabel extends Button { 

int hours; 
int minutes; 
String dayHalf; //am or pm 
Context parentActivity; 
PrayerControl parentControl; 

public PrayerTimeLabel(Context context,PrayerControl parent) { 
    super(context);     
    init(context,parent,0); 
} 

public PrayerTimeLabel(Context context, int defStyle, PrayerControl parent) { 
    //super(context, null, R.style.Button_PrayerTimeButton); 
    super(context, null, defStyle);    
    init(context,parent,defStyle); 
} 


private void init(final Context context, PrayerControl parent, int defStyle) 
{   
    parentActivity = context; 
    parentControl = parent; 
    Typeface tf = Typeface.createFromAsset(context.getAssets(),"fonts/digital.ttf"); 
    this.setTypeface(tf);  
    this.setText(false); 

    this.setOnClickListener(new OnClickListener() {   
     public void onClick(View v) { 
      TimeDialog dialogBox = parentControl.getDialogBox(); 
      dialogBox.setTime(hours, minutes, dayHalf); 
      dialogBox.show(); 
     } 
    }); 
} 


public void setTime(int hrs, int min, String half,boolean signalParent) 
{ 
    hours = hrs; 
    minutes = min; 
    dayHalf = half; 
    this.setText(signalParent); 
} 

public void setText(boolean signalParent) 
{ 
    super.setText(String.format("%02d", hours)+":"+String.format("%02d", minutes)+" "+dayHalf); 
    if(signalParent){ 
     parentControl.setPrayerTime(hours, minutes, dayHalf); 
    } 
} 

} 

और मैं निम्नलिखित शैली मेरी style.xml

<style name="Button.PrayerTimeButton" parent="@android:style/TextAppearance.Widget.Button"> 
    <item name="android:background">#000</item> 
    <item name="android:textSize">18dp</item> 
    <item name="android:textColor">#FFFF00</item> 
</style> 

विस्तारित बटन इस शैली नहीं मिल रहा है के रूप में परिभाषित किया है। क्या कुछ इस बात पर ध्यान दे सकते हैं कि मैं क्या गलत कर रहा हूं? मैंने समाधान की खोज की और this पाया। क्या कोई सुझाव दे सकता है कि

नोट: मैं शैलियों को लागू करने के लिए एक्सएमएल का उपयोग नहीं कर सकता। इसे कन्स्ट्रक्टर होना है।

संपादित करें:

के बाद वर्ग जहां इस कस्टम बटन बनाया है और प्रयोग किया जाता है। मैं कोड

public class PrayerControl extends LinearLayout { 


protected PrayerTimeLabel prayerTimeButton; 

protected String prayerName; 
protected static int counter=0; 


public PrayerControl(Context context) { 
    super(context); 
    init(context); 
} 

public PrayerControl(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    getXMLAttributes(context, attrs); 
    init(context); 
} 

    protected void getXMLAttributes(Context context, AttributeSet attrs) 
{ 
    TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.PrayerControl); 

    prayerName = a.getString(R.styleable.PrayerControl_name); 
    dayHalf = a.getString(R.styleable.PrayerControl_dayHalf); 
    hours = a.getInteger(R.styleable.PrayerControl_hours, 4); 
    minutes = a.getInteger(R.styleable.PrayerControl_minutes, 30); 

    ltrProgress = a.getInteger(R.styleable.PrayerControl_postNamazInterval, 0); 
    rtlProgress = a.getInteger(R.styleable.PrayerControl_preNamazInterval, 0); 
    intervalMax = a.getInteger(R.styleable.PrayerControl_intervalMax, 30); 


    a.recycle(); 

} 

protected void init(Context context) 
{ 
    counter++; 
    parentActivity = context; 
    this.setOrientation(LinearLayout.HORIZONTAL); 
    this.setId(counter);  

    prayerTimeButtonStyle = R.style.Button_PrayerTimeButton; 
    initializePrayerTimeButton(); 

} 


protected void initializePrayerTimeButton() 
{ 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, 
      40);   
    params.gravity = Gravity.CENTER; 
    //params.weight = 1.0f; 

    prayerTimeButton = new PrayerTimeLabel(parentActivity,prayerTimeButtonStyle,this); 
    prayerTimeButton.setTime(hours, minutes, dayHalf,false); 

    prayerTimeButton.setLayoutParams(params); 
    this.addView(prayerTimeButton); 
} 

} 
+0

getxmlAttributes क्या करता है? – Ronnie

+0

मैंने उस फ़ंक्शन को शामिल करने के लिए कोड अपडेट किया है। यह सिर्फ attr.xml फ़ाइल में आइटम्स का मान पढ़ता है। – sadaf

उत्तर

0

के कई अप्रासंगिक लाइनों नष्ट कर दिया है आप अपने निर्माता में super(context,attrs,defStyle) निर्माता के लिए एक कॉल, जो आपके View को defStyle लागू होगी बनाना चाहिए।

हालांकि आप style को द्विपक्षीय रूप से बदलने से नहीं कर सकते हैं।

+0

मैं इसे निम्नलिखित पंक्ति में कर रहा हूं सुपर (संदर्भ, शून्य, R.style.Button_PrayerTimeButton); दूसरे निर्माता में । क्या आप कृपया इस कोड के साथ गलत क्या बता सकते हैं? – sadaf

+0

क्या मैं कोड भी देख सकता हूं जहां आप उन कस्टम दृश्यों को लेआउट में जोड़ते हैं? –

+0

असल में, यह कस्टम बटन किसी अन्य कस्टम नियंत्रण का हिस्सा है जिसे तब लेआउट में जोड़ा जाता है। यही कारण है कि मैं कन्स्ट्रक्टर का उपयोग करके शैलियों को लागू करना चाहता हूं और एक्सएमएल का उपयोग नहीं करना चाहता हूं। मुझे प्रश्न अपडेट वहाँ कोड जोड़ने के लिए करते हैं ... – sadaf

3

यह एक पुरानी जवाब है: मैं एक -1 मिनट पहले के एक जोड़े मिला है और यहां

नए समाधान है:

मूल विचार निर्माता के लिए एक विशेषता पारित करने के लिए है । Applying style to views dynamically in java code

पुराने समाधान (नहीं काम):

को अपनी कक्षा में इन कंस्ट्रक्टर्स जोड़ें: पूर्ण समाधान के लिए इस लिंक की जाँच करें

public StyledButton(Context context) { 
    super(context, null, R.style.Button_PrayerTimeButton); 
//... whatever 
} 

public StyledButton(Context context, AttributeSet attrs) { 
    super(context, attrs, R.style.Button_PrayerTimeButton); 
//... whatever 
} 

public StyledButton(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, R.style.Button_PrayerTimeButton); 
//... whatever 
} 
+0

मैं इन कंस्ट्रक्टर्स लेकिन कोई फायदा नहीं :( – sadaf

+0

+1 पिछली टिप्पणी पर कहा, इस -1 के लिए धन्यवाद @adstro या तो मेरे लिए काम नहीं करता। – adstro

+0

, मैं सिर्फ देखा था इस और अद्यतन जवाब –

3

क्यों setTextAppearance(Context context, int resid); का उपयोग नहीं। यह आपको टेक्स्ट रंग, आकार, शैली, संकेत रंग, और हाइलाइट रंग सेट करने देता है।

PrayerTimeLabel कक्षा में,

private void init(final Context context, PrayerControl parent, int defStyle) 
{   
    setTextAppearance(context, R.style.Button_PrayerTimeButton); 
    ... 
} 

अधिक जानकारी के लिए इस पोस्ट देखें: setTextAppearance through code referencing custom attribute

+0

इसकी न सिर्फ TextView, TextView वह सब का सबसे सरल कारण है कि मैं TextView के लिए कोड चिपकाया है है। मुझे लगता है कि seekbar और अन्य एंड्रॉयड नियंत्रण से विरासत अन्य कस्टम नियंत्रण है। मैं उन सभी के लिए शैलियों करने में कोई समस्या हो रहा है। – sadaf

0

बग रिपोर्ट आप राज्यों से लिंक:

इसके अतिरिक्त, जब गतिशील रन पर तत्वों बनाने समय-समय पर इसका मतलब है कि आप कोड में शैली को लागू नहीं कर सकते हैं। आप इसे ध्यान में रखते दृश्य आपको बना रहे हैं के लिए एक अलग एक्सएमएल लेआउट बनाने के लिए, और यह बढ़ [...]

की जरूरत है, एक समाधान ऐसा दिखाई दे सकता:

एक xml लेआउट बनाएं अपने बटन के लिए - चलिए इसे प्रार्थना करें बटन दबाएं।xml:

<Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#FFFF00" 
     //...etc 
/> 

फिर, अपने PrayerControl कक्षा में initializePrayerTimeButton विधि में, बढ़ और PrayerControl लेआउट के लिए PrayerTimeLabel बटन जोड़ने:

//Retrieve a LayoutInflater instance that is hooked up to the current context 
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
//The LayoutInflater instantiates the layout file into a PrayerTimeLabel object 
PrayerTimeLabel button = (PrayerTimeLabel)inflater.inflate(R.layout.prayertimebutton, this, false); 
//add the PrayerTimeLabel to the PrayerControl 
this.addView(button); 

ध्यान दें कि LayoutInflater.inflate() का दूसरा पैरामीटर लिए एक संदर्भ है अभिभावक देखें (समूह)।

Google पर एक एंड्रॉइड फ्रेमवर्क इंजीनियर एडैम्प द्वारा नीचे दिए गए उत्तर, इस दृष्टिकोण को अधिक विस्तार से चर्चा करते हैं।

Why so complex to set style from code in Android

+0

हाँ, मुझे लगता है कि पढ़ने के लिए, लेकिन मैं एक्सएमएल लेआउट फाइलों का उपयोग नहीं करना चाहते हैं। इसे रचनाकारों का उपयोग करके किया जाना है। इस मुद्दे की रिपोर्ट 2010 में हुई थी , मुझे आश्चर्य है कि इस मुद्दे के लिए कोई अपडेट नहीं है .. – sadaf

+0

क्या मैं पूछ सकता हूं कि आपको रचनाकारों का उपयोग क्यों करना है? मेरी समझ आप की अपनी शैली के साथ गतिशील बटन का दृष्टांत और एक लेआउट, जो मेरा उत्तर में समाधान संभव बनाता है के लिए गतिशील रूप से इसे जोड़ने की आवश्यकता थी। बग रिपोर्ट से यह शैली को पारित करने के लिए रचनाकारों का उपयोग करके प्रकट होता है एक मृत अंत है। यह वास्तव में आश्चर्यजनक है कि इसमें कोई अपडेट नहीं किया गया है। –

+0

क्योंकि मैं लगभग 15 कस्टम नियंत्रण और इतने सारे एक्सएमएल फाइल बढ़ा-चढ़ाकर है संसाधनों के मामले में अच्छा नहीं है तो मैं सोच रहा था कुछ एक इस समस्या के लिए एक समाधान मिल गया है हो सकता है लेकिन ऐसा लगता है मैं गलत था :( – sadaf

1

यहाँ एक पुराने जवाब (और सरल) इस विषय के लिए है: https://stackoverflow.com/a/21455192/4360308

सारांश:

ContextThemeWrapper newContext = नए ContextThemeWrapper (baseContext, R.style.MyStyle); बटन = नया बटन (newContext);