मैं एक सामान्य दृश्य:एंड्रॉयड मार्जिन काम नहीं कर रहा है जब गतिशील डालने विचारों
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/contact_selected"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
android:orientation="horizontal"
android:padding="3dp">
<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Billy Bob"
/>
</LinearLayout>
जब मैं स्थिर मेरी मुख्य गतिविधि लेआउट में LinearLayout मार्कअप कॉपी, मार्जिन उम्मीद कर रहे हैं के रूप में। हालांकि, जब मैं गतिशील रूप से मुख्य गतिविधि लेआउट में दृश्य जोड़ता हूं, मार्जिन को अनदेखा कर दिया जाता है।
मुख्य लेआउट में कंटेनर एक LinearLayout है, जो एक HorizontalScrollView का एक बच्चा है है: यहाँ मैं कैसे दृश्य
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test, null);
TextView txt_title = (TextView)view.findViewById(R.id.txt_title);
txt_title.setText("DynamiC#1");
llayout.addView(view, llayout.getChildCount()-1);
View view2 = inflater.inflate(R.layout.test, null);
txt_title = (TextView)view2.findViewById(R.id.txt_title);
txt_title.setText("DynamiC#2");
llayout.addView(view2, llayout.getChildCount()-1);
डालने यहाँ यह क्या लगता है कि है। किसी भी अंतर्दृष्टि की सराहना की है।
धन्यवाद, यह एक साफ समाधान है, और यह करने का मेरा पसंदीदा तरीका है। इस सामान को सीखने वाले किसी और के लिए, ध्यान दें कि क्योंकि (देखें, पैरेंट) पैरेंट (माता-पिता के रूप में जोड़ता है) में दृश्य को बढ़ाता है, मेरे टेस्ट कोड में लाइन जहां मैं दृश्य जोड़ता हूं (यानी llayout.addView()) कारण एक त्रुटि क्योंकि दृश्य पहले ही डाला जा चुका है। मेरे मामले में मुझे अंतिम बच्चे के रूप में एक निश्चित दृश्य बनाए रखने की आवश्यकता है, और इसके सामने नए बच्चों को डालने की आवश्यकता है। इसलिए मैं inflate() से पहले अपना अंतिम दृश्य हटा देता हूं और inflate() के बाद इसे वापस जोड़ता हूं। एक जादू की तरह काम करता है। –
@ जॉनवार्ड वास्तव में आपको अभी भी addView की आवश्यकता है यदि आप उस अंतिम बूलियन पैरामीटर के साथ "inflater.inflate (R.layout.test, linearLayout, false)" का उपयोग करते हैं। – Flyview