2012-07-09 15 views
15

काम नहीं करता है मैंने शायद सभी पोस्ट और दस्तावेज पढ़े हैं लेकिन मैं अभी भी इस समस्या को हल नहीं कर सकता।addView

मैं मौजूदा (चल रहे) लेआउट को देखने के लिए addView() विधि का उपयोग करना चाहता हूं लेकिन किसी कारण से मैं नहीं कर सकता। मुझे पता है कि यह आसान और बुनियादी होना चाहिए लेकिन फिर भी मैं इसे नहीं कर सकता। तो, कृपया मेरी मदद करो।

यहाँ एक कोड है:

LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); 
     TextView text=new TextView(this); 
     text.setText("test"); 
     layout.addView(text); 

एक कोड है यही कारण है, और नतीजा यह है कि मैं केवल विचारों जो xml फ़ाइल में परिभाषित कर रहे हैं दिखाया गया है। मैंने जो नया दृश्य जोड़ा है, वहां कोई नया दृश्य नहीं है। जब मैं डीबग करता हूं तो मैं इस अतिरिक्त दृश्य को अभिभावक के बच्चे के रूप में देखता हूं जिसमें मैंने इसे जोड़ा है लेकिन यह प्रदर्शित नहीं होता है।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
       android:id="@+id/mainLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:background="@drawable/main1" > 
    <TextView android:id="@+id/app_title" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       android:text="@string/app_title" 
       android:textSize="25dp" 
       android:gravity="center_horizontal"/> 
    <TextView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:text="@string/main_screen_counter_title" 
       android:textSize="15dp" 
       android:textColor="#FFF" 
       android:gravity="center_horizontal"/> 
    <TextView android:id="@+id/frontScreenCounter" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       android:text="@string/reading" 
       android:textSize="33dp" 
       android:gravity="center_horizontal" /> 
    <GridView android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="90dp" 
    android:numColumns="3" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    android:textColor="#888" 
/> 
    </LinearLayout> 

कृपया मदद:

यहाँ

main.xml है। यह मुझे पागल कर देगा!

+0

क्या आप वह लेआउट जोड़ सकते हैं जहां आप 'टेक्स्ट व्यू' जोड़ना चाहते हैं? – Luksprog

+0

हे लुकप्रोग, यह एंड्रॉइड था: LinearLayout विशेषताओं में layout_height = "fill_parent"। अब मैंने पाठ प्रदर्शित किया है लेकिन स्क्रीन के नीचे। इसे शीर्ष पर कैसे रखा जाए? आपने मेरी मदद की है परोक्ष रूप से, लेकिन अभी भी। बहुत कुछ टीएनएक्स। – Majstor

+1

यदि आप किसी निश्चित स्थिति पर 'टेक्स्ट व्यू' रखना चाहते हैं तो आपको 'addView' विधि का एक और स्वाद का उपयोग करना चाहिए जो एक int लेता है, नई स्थिति' कहां रखना है, इस तरह कुछ: 'addView (टेक्स्ट, 0); ' – Luksprog

उत्तर

15

आप नए जोड़े गए दृश्य के लिए LayoutParameters निर्दिष्ट करना भूल गए हैं।

LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); 
    TextView text=new TextView(this); 
    text.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    text.setText("test"); 
    layout.addView(text); 

संपादित

GridView@+id/gridview की एक आईडी fill_parent के लेआउट ऊंचाई के साथ परिभाषित किया गया है, एक नया दृश्य जोड़ने के लिए कोई जगह नहीं के साथ छोड़ के साथ। इसकी ऊंचाई को wrap_content पर बदलना आपकी समस्या का समाधान कर सकता है।

दूसरों को आसानी से समाधान की पुष्टि करने में सहायता के लिए इस पोस्ट में मेरी टिप्पणी जोड़ना।

+0

मुझे कौन से पैरा को असाइन करना चाहिए? मैंने मार्जिन, चौड़ाई, ऊंचाई जोड़ने की कोशिश की लेकिन अभी भी कुछ भी नहीं .. – Majstor

+0

क्या आप अपना मेनलेआउट.एक्सएमएल पोस्ट कर सकते हैं? चूंकि अरुण का कोड भी मेरे लिए काम करता है – firefistace

+0

मैंने आपके कोड की कोशिश की और अभी भी कुछ भी नहीं। यह उस माता-पिता के बच्चे के रूप में है लेकिन प्रदर्शित नहीं होता है। अधिक विचार है? आपके प्रयास के लिए टीएनएक्स। – Majstor

-1

मुझे याद नहीं है, लेकिन शायद लेआउट को फिर से लोड करने के लिए कोई भी "ताज़ा करें" विधि है।

layout.invalidate() का प्रयास करें;

+0

कोशिश की, अभी भी कुछ भी नहीं। अधिक विचार है? – Majstor

+0

@ माजस्टोर 'अमान्य();' शीर्ष स्तर के लेआउट पर कॉल किया जाना चाहिए, क्या आपने इसे उस ऑब्जेक्ट से कॉल किया था? – ForceMagic

+2

addView कॉल पहले से ही अमान्य है। –

1

मुझे एक ही समस्या थी और कारण खोजने में कई बार बर्बाद हो गया।

मेरी गलती यह थी कि मैं एक कस्टम व्यू जोड़ रहा था जिसमें match_parent ऊंचाई के रूप में सेट था।

मैं ने इस मूर्खतापूर्ण कष्टप्रद गलती किया था किसी को कुछ कीमती समय :)

TextView tv = new TextView(this); 
lytMarks.addView(tv); 


CustomView cv = new CustomView(this, someObject); 
lytMarks.addView(cv); // <-- This one had height = match_parent!! 


EditText et = new EditText(this); 
lytMarks.addView(et); 
0

आपका LinearLayout सामग्री माता पिता के दृश्य खत्म हो गया है को बचाने के लिए उम्मीद है। तो आपको स्क्रॉलव्यू का उपयोग करने की आवश्यकता है। इस तरह:

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<LinearLayout 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/main1" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/app_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="@string/app_title" 
     android:textColor="#FFF" 
     android:textSize="25dp" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:gravity="center_horizontal" 
     android:text="@string/main_screen_counter_title" 
     android:textColor="#FFF" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/frontScreenCounter" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="@string/reading" 
     android:textColor="#FFF" 
     android:textSize="33dp" /> 

    <GridView 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:columnWidth="90dp" 
     android:gravity="center" 
     android:horizontalSpacing="10dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:textColor="#888" 
     android:verticalSpacing="10dp" /> 
</LinearLayout> 
</ScrollView> 
संबंधित मुद्दे