2012-08-15 13 views
22

मैंने इसे विभिन्न विभिन्न कोणों से मारा है। असल में यह बात है:एंड्रॉइड: टेक्स्ट व्यू के लिए लेआउट पैराम्स दृश्य गायब हो जाता है, प्रोग्रामेटिक

मैंने एक्सएमएल में एक इंटरफेस के लिए एक टेम्पलेट डाला है जिसे प्रोग्रामेटिक रूप से चलाने की आवश्यकता है, इसलिए इसे चलाने के दौरान गतिशील रूप से पॉप्युलेट किया जा रहा है।

यहां समस्या यह है कि एक्सएमएल टेक्स्ट व्यू में कुछ लेआउट बदलाव (जो काम करते हैं) और आवश्यक हैं। लेकिन अगर मैं वास्तव में उन्हें कोड में सेट करता हूं, तो टेक्स्ट व्यू भी दिखाई नहीं देता है।

(TextView, वैसे, एक TableRow अंदर है, इस प्रकार कॉल वजन के नीडिंत है।) मैं कोड के लिए संदर्भ के रूप में उपयोग करने के लिए पहले तैयार किया गया है, एक्सएमएल टेम्पलेट यह है, और यह सिर्फ ठीक काम करता है:

<TextView 
    android:id="@+id/txtviewWhiteBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:background="@drawable/textview_9patch_white" 
    android:gravity="center" 
    android:text="75" 
    android:textColor="@android:color/black" 
    android:layout_margin="20dp" 
    android:padding="20dp" 
    android:textSize="40dp" /> 

जैसा मैंने कहा, वह पूरी तरह से बताता है। जब मैं कोड में एक ही लेआउट चलाता हूं, और लेआउटपैम लागू करता हूं, तो टेक्स्ट व्यू गायब हो जाता है।

यहाँ प्रासंगिक स्निपेट है:

int textViewExampleID = 9001; 

private TextView txtviewExample = new TextView(this); 

private void buildTextView(){ 
    LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f); 
    txtviewExample.setId(textViewExampleID); 
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white); 
    txtviewExample.setGravity(Gravity.CENTER); 
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black)); 
    paramsExample.setMargins(20, 20, 20, 20); 
    txtviewExample.setPadding(20, 20, 20, 20); 
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample"); 

    //if I comment out the following line, this TextView displays. 
    //if I leave it in, it doesn't display. 
    txtviewExample.setLayoutParams(paramsExample); 
} 

मैं वहाँ LayoutParams के लिए उपलब्ध वर्गों के सभी प्रकार है एहसास है, और मैं उन लोगों के साथ सभी LinearLayout.LayoutParams खेलने किया गया है, TableView.LayoutParams, RelativeLayout.LayoutParams, LayoutParams बस अपने आप ... कोई फर्क नहीं पड़ता कि मैं किस कोशिश कर रहा हूं, "setLayoutParams" को कॉल करने का कोई भी प्रयास पूरे टेक्स्ट व्यू को प्रस्तुत करता है। मैंने यहां मंचों को खराब कर दिया है, और मुझे जवाब नहीं मिला है। यह असामान्य नहीं हो सकता है।

+4

आप की कोशिश की है 'TableRow.LayoutParams' http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html –

+1

मैं बस किया था, (यह याद किया कल रात टिप्पणी, दरनीट) और यह काम किया। धन्यवाद आदमी – Octoth0rpe

+0

आपका स्वागत है :) –

उत्तर

84

अच्छी तरह से है कि मैं कोशिश करेगा हो सकता है, कि दर्दनाक था, लेकिन मैं अंत में यह पता लगा मिला है। याद रखने की सबसे महत्वपूर्ण बात (जिसे मैंने अभी महसूस किया) LayoutParams के सभी असंख्यों में से एक है, आपको उस दृश्य का उपयोग करने की आवश्यकता है जो उस दृश्य के माता-पिता से संबंधित है जो आप काम कर रहे हैं, वास्तविक दृश्य नहीं।

तो मेरे मामले में, मैं TextView मार्जिन काम करने की कोशिश कर रहा था, लेकिन इसे TableRow के अंदर रखा जा रहा था। एक साधारण परिवर्तन सुनिश्चित किया गया कि LayoutParams के प्रकार इस्तेमाल किया जा रहा TableRow के लिए लोगों को इस प्रकार थे:

private void buildTextView(){ 
    // the following change is what fixed it 
    TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f); 
    txtviewExample.setId(textViewExampleID); 
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white); 
    txtviewExample.setGravity(Gravity.CENTER); 
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black)); 
    paramsExample.setMargins(20, 20, 20, 20); 
    txtviewExample.setPadding(20, 20, 20, 20); 
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample"); 
    txtviewExample.setLayoutParams(paramsExample); 
} 

धन्यवाद दोस्तों, उम्मीद है कि इस काम में लाइन नीचे किसी और के लिए, आ जाएगा के रूप में मैं का एक बहुत देखा मंचों में अर्ध-संबंधित प्रश्न यहां, लेकिन ऐसा नहीं है जो वास्तव में समस्या को परिभाषित करता है।

+5

आपको धन्यवाद! मेरे पास एक ही समस्या थी, ऐसा इसलिए था क्योंकि मैं TableRow.LayoutParams के बजाय LinearLayout.LayoutParams का उपयोग कर रहा था ... गूंगा गलती लेकिन आपने मुझे बहुत निराशा से बचाया! – RyanG

+0

मैं बस आपको धन्यवाद कहना चाहता हूं! आपने मेरा दिन बचा लिया! – Zookey

+3

दो घंटों के लिए मैंने इसे हल करने की कोशिश की। आपको धन्यवाद। – arenaq

0

लेआउटपरैम में तीसरा चर क्या है, क्या वह अल्फा है? क्या होता है यदि आप paramsExample.setMargins

अंत में क्या होता है तो setLayoutParams के बाद txtviewExample.setVisible(View.Visible) पर क्या होता है?

उन चीजों यदि आप नहीं

+0

लेआउटपरैम में तीसरा संदर्भ 'एंड्रॉइड के बराबर वजन के लिए है: लेआउट_वेट =" 1.0 "' एक्सएमएल में हिस्सा। और '.setVisible (View.VISABLE); 'काम नहीं किया। हालांकि धन्यवाद। – Octoth0rpe

+0

FWIW, मैंने 'txtviewExample.setLayoutParms (paramsExample) का उपयोग न करने का भी प्रयास किया है; और फिर लेआउट में वास्तविक addView के दौरान, इसे द्वितीयक पैरामीटर के रूप में चिह्नित करें। अभी भी काम नहीं किया: 'tblrow.addView (txtviewExample, paramsExample);' – Octoth0rpe

+0

'setText' में मुझे लगता है कि आपके पास वास्तविक स्ट्रिंग चर है? क्या इस चर के पास कोई डेटा होता है जब इसे बुलाया जाता है? – CQM

1
private TextView txtviewExample = new TextView(this); 

private void buildTextView(){ 
    LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f); 
    txtviewExample.setId(textViewExampleID); 
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white); 
    txtviewExample.setGravity(Gravity.CENTER); 
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black)); 
    paramsExample.setMargins(20, 20, 20, 20); 
    txtviewExample.setPadding(20, 20, 20, 20); 
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample"); 

    setContentView(txtviewExample);//when you don't use SETTER method for TextView you can't view the desireable text on the UI// 
} 

// का उपयोग

+0

यूआई स्क्रीन – killer

+0

पर पाठ को दृश्यमान बनाने के लिए setContentView (txtviewExample) का उपयोग करें, क्या आप पहले से ही उत्तर दिए गए प्रश्न का उत्तर देने के बजाय टिप्पणी नहीं कर सकते ..? – NREZ

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