简体   繁体   中英

Changing the width of AutoCompleteTextView in Android

I've been stumped on this for a few days. I have an autoCompleteTextView and a button split 50/50 in my layout. The autocomplete works, but the suggestions only take up half of the screen (equivalent to the size that my textView takes up). How could I change this to take up the whole width?

Pardon my terrible paint skills:

Blue = AutoCompleteTextView Red = desired autocomplete suggestions

This seems like a trivial problem, but simply changing the width to fill_parent doesn't work. Any ideas xml or programatically?

SOLUTION: myAutoCompleteTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT)

在此处输入图片说明

EDIT: My first question seems to be solved, but now if I switch my button and my autoCompleteTextView I get another weird problem. The width starts at where the textView starts. Leaving me with an undesireable result.

SOLUTION: See edit 1 from user: vikram

在此处输入图片说明

You can set this attribute in the description of this widget using the attribute:

android:dropDownWidth="match_parent"

If you would like to do this in java 'programmatically', use the method setDropDownWidth(int) :

myAutoCompleteTextView.setDropDownWidth(int)

Edit 1:

Point pointSize = new Point();

getWindowManager().getDefaultDisplay().getSize(pointSize);

myAutoCompleteTextView.setDropDownWidth(pointSize.x);

I find setDropDownAnchor() to be much more convenient.

Just pass the id of some view and drop down view will match its width and position (no matter where the actual text view is placed).

In your example that could be the parent of both: button and text view.

In my case AutoCompleteTextView occupied a whole width. Looking at change position of spinner dropdown list I set

android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"

to create left and right margins of a list.

在此处输入图片说明

您还可以调整宽度的起始位置:

myAutoCompleteTextView.setDropDownHorizontalOffset(int offset)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM