Android:如何使AutoCompleteTextView下拉列表自动换行成两行或更多行

17

我正在编写一个使用 AutoCompleteTextView 的 Android 应用程序,就像 API 示例一样。问题在于我的文本太长,在下拉列表出现时需要换行。我有一个自定义的 list_item.xml,但只显示一行文本。如果与 spinner 一起使用,list_item.xml 布局会换行为 2 行,但与 AutoCompleteTextView 不兼容。

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"
        };
}

主要的.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>

为什么答案没有被接受? - viplezer
2个回答

40

将 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
对我来说,当选择一个项目(onItemSelected)时,它停止了响应并停止接收通知。http://stackoverflow.com/questions/25751648/autocompletetextview-stops-listening-to-setonitemclicklistener - Siddharth

3

简单的方法:
adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.two_line_list_item, android.R.id.text1, ....);

android.R.layout.two_line_list_item 是一个很容易更改的布局。


我认为在这里应该使用Activity上下文而不是应用程序上下文:https://dev59.com/UGw05IYBdhLWcg3wVwc4#7298955 - Aritz
我认为只有在一个布局中有两个独立的文本时才会使用两行。如果要使单个文本换行,您必须创建自己的布局。 - Noor Ali Butt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接