安卓自定义布局用于AutoCompleteTextView

8

可能我错过了一些简单的东西,但是我有一个包含一些非常长的项目的AutoCompleteTextView。当其中一个被点击时,它会在EditText中正确显示文本,并跨越几行。

然而,我希望它在弹出窗口中也能够以多行的形式显示,以便用户可以看到他们正在选择哪个项目。

这是我的自定义布局:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ellipsize="none"
    android:maxLines="100"
    android:scrollHorizontally="false" />

这是数组和适配器的初始化:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.dropdown_item_wrap_line,
                getResources().getStringArray(R.array.building_descriptions));

mBuildingDesc.setAdapter(adapter);

你需要在一个元素内有多行吗?我的意思是,AutoCompleteTextView中的单个视图应该包含两行,并且您将使用第二行来显示其他信息?我说得对吗? - Arif Nadeem
不好意思,我只是想要一个视图来包裹内容而不是截断它,因为我的项目可能会很长(例如“Lorem ipsum dolor sit amet,consectetur adipiscing elit。Aliquam ultricies tempus neque,nec egestas erat tincidunt et。”) - Carrie Hall
1个回答

13
我的猜测是你的AutoCompleteTextView被限制为singleLine,因此使用具有默认TextView的自定义布局应该可以很好地解决问题。
你需要创建一个名为custom_item.xml的新Layout,然后像这样操作...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView 
        android:id="@+id/autoCompleteItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textSize="14sp"    
        />

</LinearLayout>

在使用 AutoCompleteTextView 上使用适配器时,执行以下操作:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
ArrayAdapter<String> adapter  = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, StringArray);
textView.setAdapter(adapter);

这个是可行的,但是如果你要获取项目的文本,可能会出现问题。所以首先设置setOnItemClickListener,然后参数View view不再是TextView而是LinearLayout。 - cutiko
我不这么认为,我使用了自定义的ArrayAdapter,它适用于单行,尽管我没有检查过自定义布局。我将会去检查并在此处更新 :) - Ashu Kumar

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