2010-02-18 68 views
12

我正在編寫一個Android應用程序,它與API示例一樣使用AutoCompleteTextView。問題在於我的文本太長,需要在下拉菜單時換行。我有一個自定義list_item.xml,但只顯示一行文本。如果與微調器一起使用,則list_item.xml佈局將包裝爲2行,但不包含AutoCompleteTextView。Android:使AutoCompleteTextView下拉列表換行至2行或更多

main.java

public class main extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES); 
     textView.setAdapter(adapter); 
    } 

     static final String[] COUNTRIES = new String[] { 
      "This is the first line that is too long and it needs to wrap content", 
      "Albania", "Algeria", "American Samoa", "Andorra", 
      "This is a second line that also needs to wrap its content", 
       "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium" 
     }; 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Country" /> 

    <AutoCompleteTextView android:id="@+id/autocomplete_country" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp"/>   
</LinearLayout> 

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textSize="16sp" 
    android:textColor="#000"  
    > 
</TextView> 
+0

爲什麼不能得到答案? – viplezer 2013-12-07 14:48:29

回答

28

裹在佈局​​TextView的,並使用你的ArrayAdapter

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/item" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:textSize="16sp" 
     android:textColor="#000"  
     /> 

</LinearLayout> 

而對於ArrayAdapter新的呼叫:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.item, COUNTRIES); 
    textView.setAdapter(adapter); 
+1

它爲我工作。非常感謝你 – Sakthimuthiah 2013-07-02 06:34:47

+1

對我來說,它停止了對一個項目的選擇(onItemSelected)的反應,停止獲取通知。 http://stackoverflow.com/questions/25751648/autocompletetextview-stops-listening-to-setonitemclicklistener – Siddharth 2014-09-10 15:33:34

+0

Thanx,適合我。 – Fire 2015-03-25 06:37:59

4

簡單的方法:
適配器=新ArrayAdapter(getApplicationContext(),android.R.layout.two_line_list_item,android.R.id.text1, ....);

android.R.layout.two_line_list_item這很容易改變。

+0

我認爲你應該在這裏使用Activity Context而不是Application Context:http://stackoverflow.com/a/7298955/1199132 – 2014-08-20 08:43:08

相關問題