2012-07-30 112 views
5

微調框有一些問題。根據我的日期,我必須添加到TableRow a TextViewEditTextSpinner。我必須在Spinner中顯示的數組有點長。我測試了我的代碼與短文本的數組,它看起來像這樣:微調框不能正常工作

enter image description here

這裏的一個問題是,微調不是fill_parent

如果我把我的數組微調器,它看起來是這樣的:

enter image description here

在這種情況下,微調看起來並不像一個微調和EditText上是不可見的了。當我選擇了微調,看來這樣的觀點:

enter image description here

在這裏我需要顯示陣列的所有文本。 這是我的代碼:

TableRow.LayoutParams lp = new TableRow.LayoutParams(
      TableRow.LayoutParams.FILL_PARENT , TableRow.LayoutParams.WRAP_CONTENT); 
tablerow_product[i] = new TableRow(viewToLoad.getContext()); 
tablerow_product[i].setLayoutParams(lp); 

product_spinner[i] = new Spinner(viewToLoad.getContext()); 
product_spinner[i].setLayoutParams(lp); product_spinner[i].setBackgroundResource(R.drawable.spinner_selector); 
String[] proba={"red","blue"}; //first image is with this test array 
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(viewToLoad.getContext(), com.Orange.R.layout.my_spinner_textview,spinnerArray);          spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item); 
product_spinner[i].setAdapter(spinnerArrayAdapter); 
tablerow_product[i].addView(product_spinner[i]);           Themes_TableLayout.addView(tablerow_product[i],new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,     TableRow.LayoutParams.WRAP_CONTENT)); 

和my_spinner_textview.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    style="?android:attr/spinnerItemStyle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@drawable/textorange_selected" 
    android:gravity="left" 
    android:singleLine="false" 
    android:ellipsize="end"/> 

誰能幫助我解決這個問題?任何想法是受歡迎的。提前致謝。

+0

一次檢查這個鏈接http://android-coding.blogspot.i n/2011/12/dynamic-change-content-of-spinner.html – shassss 2012-07-30 11:37:44

+0

這個問題的答案解決了我的問題:http://stackoverflow.com/questions/2325242/android-how-do-i-add-1 -views-in-one-cell-for-tablerow – Gabrielle 2012-07-31 06:35:49

+0

@Bhargavi我會很快用我的代碼發表一個答案。 – Gabrielle 2012-08-31 07:06:56

回答

2

對於我的問題,下面的代碼

class SpinnerAdapter extends ArrayAdapter<String> 
{ 
    Context context; 
    List<String> items; 
    public SpinnerAdapter(final Context context, 
      final int textViewResourceId, List<String> vendor_name) { 
     super(context, textViewResourceId, vendor_name); 
     this.items = vendor_name; 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater inflater = LayoutInflater.from(context); 
      convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false); 
     } 
     // android.R.id.text1 is default text view in resource of the android. 
     // android.R.layout.simple_spinner_item is default layout in resources of android. 

     TextView tv = (TextView) convertView.findViewById(android.R.id.text1); 
     tv.setText(items.get(position)); 
     tv.setTextColor(Color.BLACK); 
     tv.setTextSize(9); 
      return convertView; 
    } 
    } 

我發現這個解決方案:

Spinner language = (Spinner) findViewById(com.Orange.R.id.current_language_text); 

ArrayAdapter adapter = new ArrayAdapter(this, 
       com.Orange.R.layout.my_spinner_textview, languages); 
adapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item); 
language.setAdapter(adapter); 

,其中語言是String[]my_spinner_textview.xml是:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/textview_spinner" 
    style="?android:attr/spinnerItemStyle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@drawable/textorange_selected" 
    android:paddingLeft="5dp" 
    android:singleLine="true" 
    android:ellipsize="end" 
/> 
1

我有一個想法,你可以創建自定義適配器。 使用此SpinnerAdapter類