2012-09-14 22 views
7

के रूप में एक्सएमएल लेआउट का प्रोग्रामेटिक रूप से एंड्रॉइड बटन बनाएं, मेरे पास एक लाइनरलाइट है जिसमें टेक्स्ट व्यू है, और हमेशा होगा। टेक्स्टव्यू के नीचे हमेशा कम से कम एक बटन भी होगा, लेकिन कुछ परिस्थितियों में एक से अधिक हो सकते हैं।टेम्पलेट

मैं प्रोग्रामेटिक रूप से आवश्यकतानुसार कई बटन सफलतापूर्वक बना और जोड़ सकता हूं। मैं प्रोग्राम्सैटिक रूप से इन बटनों के लिए जो भी उपस्थिति संबंधित पैरामीटर/विकल्प की आवश्यकता है, सफलतापूर्वक सेट कर सकता हूं।

समस्या यह है कि मुझे नहीं पता कि प्रोग्रामेटिक रूप से बनाए गए बटन को कैसे बताना है कि इसे एक्सएमएल संसाधन फ़ाइल का उपयोग करना चाहिए, जिसमें इन पैरामीटर को प्रोग्रामेटिक रूप से सेट करने के बजाय उपस्थिति और लेआउट पैरामीटर शामिल हैं।

मैंने इसी तरह के नामित प्रश्नों को देखा है और एपीआई के साथ समय-समय पर गड़बड़ करने में कोई फायदा नहीं हुआ है।

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

private TextView textView; 
private SomeObject someObject; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View scrollView = inflater.inflate(R.layout.fragment_play_game, container, false); 
    textView = (TextView) scrollView.findViewById(R.id.game_data_text); 
    textView.setText(someObject.getTextForTextView()); 

    LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.game_data_container); 
    for (String optionText : someObject.getTextForButtons()) { 
     layout.addView(createOptionButton(optionText, layout)); 
    } 
    return scrollView; 
} 

private View createOptionButton(String optionText, LinearLayout layout) { 
    Button optionButton = new Button(this.getActivity()); 
    // set button layout/options here, somehow?? 
    optionButton.setText(optionText); 
    return optionButton; 
} 

टुकड़ा के लिए मेरे एक्सएमएल लेआउट फ़ाइल इस तरह दिखता है (यह इस है:
यहाँ मैं क्या उम्मीद है कि मेरे लिए थोड़ा स्पष्ट स्पष्टीकरण कर देगा कि ऐसा करने के लिए कोशिश कर रहा हूँ की एक सन्निकटन है LinearLayout कि मैं बटन) जोड़ने के लिए कोशिश कर रहा हूँ: इसके अलावा

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

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/game_data_container" 
     etc... > 

     <TextView 
      android:id="@+id/game_data_text" 
      etc... /> 

    </LinearLayout> 

</ScrollView> 

, अगर मैं बटन के लिए एक XML लेआउट फ़ाइल बनाने के लिए कर रहा हूँ (इसे कहते custom_button.xml) यह कुछ इस तरह देखना चाहिए की सुविधा देता है ?:

<?xml version="1.0" encoding="utf-8"?> 
    <Button xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/play_game_option_button" 
     etc... /> 

अपडेट: इस एक के साथ

Button optionButton = new Button(this.getActivity()); 

:
बस क्या MrFox @ के बारे में बात कर रही है पर थोड़ा विस्तार करने के लिए, क्या मैं इसे काम कर पाने के लिए किया था इस लाइन की जगह था

Button optionButton = (Button) inflater.inflate(R.layout.play_game_option_button, layout, false); 

... जो केवल एक बटन लेआउट (बटन टेम्पलेट) युक्त एक XML फ़ाइल को बढ़ाता है। इस मामले में, यह उस फ़ाइल का मूल दृश्य देता है, जो केवल बटन है क्योंकि फ़ाइल में बटन के ऊपर कोई पैरेंट नहीं है।

हालांकि, अगर मैंने अंतिम बूलियन मान (अटैचमेंट पैरेंट) को सत्य पर सेट किया था, तो यह उस रूट कंटेनर को वापस कर देता था जिसमें बटन होगा (जो कॉल में पारित किया गया 'लेआउट' चर है) ।

अब मैं इस टेम्पलेट का उपयोग करने के रूप में कई बटन उत्पन्न कर सकता हूं।

उत्तर

5

क्या आपने ऐसा लेआउट बनाने का विचार किया है जो केवल लागू XML शैलियों के साथ बटन है और फिर इसे अपने रैखिक लेआउट में फुला रहा है?

कुछ की तरह:

inflater.inflate (R.layout।स्टाइलबटन, माइलिनरलाउट, सच);

+1

धन्यवाद प्रतिक्रिया के लिए, मैं क्या आपके सुझाव के साथ प्रयोग करना की कोशिश की है, लेकिन मैं वास्तव में समझ में नहीं आता आपके सुझावों को कैसे कार्यान्वित करें। मैंने अपने मूल प्रश्न को अधिक जानकारी के साथ अपडेट किया है। – Rob

+0

आप और मैं दोनों –

+0

मेरे उत्तर पर विस्तार के लिए @Rob धन्यवाद! –

1

अपनी गतिविधि में के तहत /res/layout/my_button_layout.xml

<Button xmlns:android="http://schemas.android.com/apk/res/android" 
    ... /> 

कोड आपके बटन के लिए एक्सएमएल

myButton = (Button)inflate.inflate(R.layout.my_button_layout, null); 
myView.addView(myButton); 
संबंधित मुद्दे