2013-04-22 12 views
5

मैंने मेन.एक्सएमएल फ़ाइल में पहले दो मेनू आइटम्स बनाकर मैन्युअल रूप से एक्शन बार में दो स्पिनर बनाए हैं। लाइनएक्शनबार चौड़ाई में दो स्पिनर मेनू आइटम

cSpinner.setAdapter(ArrayAdapter.createFromResource(this, 
      R.array.category_data, 
      android.R.layout.simple_spinner_dropdown_item) 
      ); 

मैंने उनके लिए ऐरे संसाधन सेट किया है। ये चीजें वास्तव में काम करती हैं लेकिन समस्या यह है कि बाएं स्पिनर संसाधन स्ट्रिंग इतनी बड़ी है कि सही स्पिनर का एक छोटा सा हिस्सा देखा जा सकता है।

http://s1.directupload.net/images/130422/88wtvfft.png

मैं cSpinner.setLayoutParams(new Spinner.LayoutParams(60, 20)); तरह बातें की कोशिश की है या '

ViewGroup.LayoutParams params = pView.getLayoutParams(); 
     params.width = 100; 
      cspinner.setLayoutParams(params); 

लेकिन उनमें से कोई भी काम करते हैं। दूसरा भी प्रोग्राम क्रैश करने देता है। क्या अब आप एक टिप जानते हैं कि मैं इस समस्या को कैसे हल कर सकता हूं?

+0

स्पिनर की लेआउट फ़ाइल के अंदर मानों को सेट करने का प्रयास करें। –

+0

दो स्पिनर वास्तव में layout.xml फ़ाइल में स्पिनर नहीं हैं, वे menu.xml में दो मेनू आइटम हैं इसलिए मैं उन्हें मान सेट नहीं कर सकता। – Elektropepi

उत्तर

10
इस आप नीचे

enter image description here

enter image description here


कोड की तरह ActionBar में कस्टम लेआउट जोड़ने के लिए के लिए

यहाँ है

public class MainActivity extends Activity { 

    final String[] choices = { "Android", "iOS", "RIM" }; 

    private Spinner Spin1; 
    private Spinner Spin2; 

    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       MainActivity.this, android.R.layout.simple_dropdown_item_1line, 
       choices); 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setCustomView(R.layout.actionbar_item); 
     actionBar.setDisplayShowTitleEnabled(false); 
     actionBar.setDisplayShowCustomEnabled(true); 
     actionBar.setDisplayUseLogoEnabled(false); 
     actionBar.setDisplayShowHomeEnabled(false); 

     Spin1 = (Spinner) findViewById(R.id.spinner1); 
     Spin2 = (Spinner) findViewById(R.id.spinner2); 

     Spin1.setAdapter(adapter); 
     Spin2.setAdapter(adapter); 
    } 

} 

actionbar_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:orientation="horizontal" 
android:weightSum="5" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    android:text="my App name" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="#000000" /> 

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 

<Spinner 
    android:id="@+id/spinner2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 

</LinearLayout> 
+0

धन्यवाद, यह पूरी तरह से काम करता है, बिल्कुल वही चीज़ जो मैं ढूंढ रहा था। – Elektropepi

+0

समझ में नहीं आता क्यों actionBar.setCustomView (R.layout.actionbar_item); और इससे पहले आप setContentView (R.layout.activity_main) का उपयोग कर सकते हैं; – duggu

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