2013-03-26 75 views
5

我可能失去了一些东西简单,但我有一个AutoCompleteTextView包含一些很长的项目。单击时,它将在EditText中正确显示文本,并跨越多行。的Android定制布局AutoCompleteTextView

不过,我希望它是在上弹出,以及使用户可以看到他们正在选择哪个项目多行。

这里是我的自定义布局:

<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); 
+0

你需要元素中有多行?我的意思是AutoCompleteTextView中的单个视图应该包含两行,并且您将使用第二行来显示一些其他信息?我是对的? – 2013-03-26 16:25:22

+0

对不起,不,我只是说对于环绕,而不是切断一个观点,我的项目可能会很长(如“Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。Aliquam ultricies坦帕斯预期值,也制造业egestas ERAT tincidunt等。”) – 2013-03-26 16:41:52

回答

9

我的猜测是,你AutoCompleteTextView仅限于singleLine,因此使用自定义布局默认TextView应该工作整齐。

您将需要一个新的Layout并将其命名为custom_item.xml,然后像这样做...

<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); 
+0

这完美的作品!非常感谢! – 2013-03-27 11:35:44

+0

这种方式的工作原理是如何的,如果你要获得物品的文字,你可能会遇到问题。因此,首先设置setOnItemClickListener,然后param View视图不再是TextView,而是LinearLayout。 – cutiko 2015-08-27 15:58:57

+0

我不这么认为,我使用自定义的数组适配器,它与单行工作,虽然我没有检查自定义布局。我会检查并更新它在这里:) – 2017-09-27 11:29:09