Android xml文件中的MultiAutoCompleteTextView setDropDownBackgroundResource

Android xml文件中的MultiAutoCompleteTextView setDropDownBackgroundResource,android,android-layout,autocompletetextview,Android,Android Layout,Autocompletetextview,有没有办法在xml布局中为MultiAutoCompleteTextView设置下拉背景?还是必须始终以编程方式完成?在您为多自动完成创建的适配器中,使用自定义布局文件,而不是android的简单列表项目1 这样,您就可以更改下拉菜单的布局并添加所需的背景 下面是一个使用自定义xml布局的多重自动完成的清晰示例: 主活动,MainActivity.java: public class MainActivity extends Activity { private MultiAutoCompl

有没有办法在xml布局中为MultiAutoCompleteTextView设置下拉背景?还是必须始终以编程方式完成?

在您为
多自动完成
创建的适配器中,使用自定义布局文件,而不是android的
简单列表项目1

这样,您就可以更改下拉菜单的布局并添加所需的背景

下面是一个使用自定义xml布局的
多重自动完成
的清晰示例:

主活动,MainActivity.java

public class MainActivity extends Activity {

 private MultiAutoCompleteTextView multiAutoComplete;
 private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // get the string-array defined in strings.xml
    String[] items = getResources().getStringArray(R.array.itemlist);

    //set your custom xml file to be able to edit it
    adapter = new ArrayAdapter<String>(this,android.R.layout.custom_drop_down_menu,items);

    //multiAutoComplete field
    multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);

    // set adapter for the multi auto complete fields
    multiAutoComplete.setAdapter(adapter);

    // specify the minimum type of characters before drop-down list is shown
    multiAutoComplete.setThreshold(2);

    // comma to separate the different colors
    multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    //toast message when the user clicks an item of the drop-down list
    multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
             Toast.makeText(getBaseContext(),"you added: "+arg0.getItemAtPosition(arg2),Toast.LENGTH_LONG).show();
        }
    });
}
公共类MainActivity扩展活动{
私有多重自动完成文本视图多重自动完成;
专用阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取strings.xml中定义的字符串数组
String[]items=getResources().getStringArray(R.array.itemlist);
//将自定义xml文件设置为可以对其进行编辑
adapter=new ArrayAdapter(这是android.R.layout.custom_下拉菜单项);
//多重自完备场
multiAutoComplete=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoComplete);
//为多自动完成字段设置适配器
multiAutoComplete.setAdapter(适配器);
//在显示下拉列表之前,指定字符的最小类型
多重自动完成。设置阈值(2);
//用逗号分隔不同的颜色
multiAutoComplete.setTokenizer(新的MultiAutoCompleteTextView.CommaTokenizer());
//当用户单击下拉列表中的某一项时显示toast消息
multiAutoComplete.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
您添加了:“+arg0.getItemAtPosition(arg2),Toast.LENGTH_LONG.show()”;
}
});
}
}

自定义多重自动完成布局,自定义下拉菜单。xml

 <!-- add your background image here -->   

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingLeft="6dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="#000"
    android:background = "@drawable/imagename"/>

字符串文件(用于添加字符串数组项),Strings.xml:


多自动完成项目
项目1
项目2
项目3
项目4
项目5
主活动布局,Activity_Main.xml:


<resources>
    <string name="multi_auto_complete_items">Multi Auto Complete items</string>

    <string-array name="itemlist">
        <item >item1</item>
        <item >item2</item>
        <item >item3</item>
        <item >item4</item>
        <item >item5</item>
     </string-array>

</resources>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <TextView
          android:id="@+id/text2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="30dp"
          android:text="@string/multi_auto_complete_items"
          android:textAppearance="?android:attr/textAppearanceMedium" />

       <MultiAutoCompleteTextView
          android:id="@+id/multiAutoComplete"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/text2"
          android:layout_marginTop="20dp"
          android:ems="13" />

</RelativeLayout>