Android:将AutoCompleteTextView下拉包装换成2行以上 [英] Android: Make AutoCompleteTextView dropdown wrap to 2 lines or more

查看:118
本文介绍了Android:将AutoCompleteTextView下拉包装换成2行以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用AutoCompleteTextView的Android应用程序,就像API示例一样。问题是我的文本太长,当下拉列表发生时需要换行。我有一个自定义的list_item.xml,但只显示一行文本。如果与微调框一起使用,则list_item.xml布局将包装为2行,但不包含AutoCompleteTextView。

I am writing an Android app that uses an AutoCompleteTextView just like the API example. The problem is that my text is too long and needs to wrap when the dropdown occurs. I have a custom list_item.xml, but only one line of text displays. The list_item.xml layout wraps to 2 lines if used with a spinner, but not with the AutoCompleteTextView.

main.java

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

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>


推荐答案

将TextView包装在布局中,并将其用于ArrayAdapter

Wrap the TextView in a layout and use that for your 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);

And the new calls for the 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);

这篇关于Android:将AutoCompleteTextView下拉包装换成2行以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆