安卓:请AutoCompleteTextView下拉换到2线以上 [英] Android: Make AutoCompleteTextView dropdown wrap to 2 lines or more

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

问题描述

我写一个Android应用程序,使用一个AutoCompleteTextView就像API的例子。问题是,我的文字太长,需要在下拉发生缠绕。我有一个自定义list_item.xml,但只有一行文字显示。所述list_item.xml布局换行到2行如果使用用旋涂器,但不与AutoCompleteTextView

main.java

 公共类主要的扩展活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        AutoCompleteTextView的TextView =(AutoCompleteTextView)findViewById(R.id.autocomplete_country);
        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,R.layout.list_item,国家);
        textView.setAdapter(适配器);
    }

        静态最终的String [] COUNTRIES =新的String [] {
            这是过长的第一行,并且需要来包装内容,
          阿尔巴尼亚,阿尔及利亚,美属萨摩亚,安道尔,
          这是也需要包其内容的第二线,
              巴林,孟,巴巴多斯,白,比利时
        };
}
 

main.xml中

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=5DP>
    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=国家/>

    < AutoCompleteTextView机器人:ID =@ + ID / autocomplete_country
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =5DP/>
< / LinearLayout中>
 

list_item.xml

 < XML版本=1.0编码=UTF-8&GT?;
< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=10dp
    机器人:TEXTSIZE =16SP
    机器人:文字颜色=#000
    >
< / TextView的>
 

解决方案

裹TextView的在布局和使用,为您的ArrayAdapter

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的TextView
        机器人:ID =@ + ID /项目
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:填充=10dp
        机器人:TEXTSIZE =16SP
        机器人:文字颜色=#000
        />

< / LinearLayout中>
 

而对于一个ArrayAdapter新的呼叫:

  

AutoCompleteTextView的TextView =(AutoCompleteTextView)findViewById(R.id.autocomplete_country);

     

ArrayAdapter适配器=新的ArrayAdapter(getActivity(),R.layout.list_item,R.id.item,国家);

     

textView.setAdapter(适配器);

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

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>

解决方案

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>

And the new calls for the ArrayAdapter:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);

ArrayAdapter adapter = new ArrayAdapter(getActivity(), R.layout.list_item, R.id.item, COUNTRIES);

textView.setAdapter(adapter);

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

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