Android AutoCompleteTextView 下拉位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26401412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android AutoCompleteTextView DropDown Position
提问by Figen Güng?r
I am using following code for auto complete however, dropdown items comes above the AutoCompleteTextView
. I want it to show up beneath the AutoCompleteTextView
. How can I do that?
我正在使用以下代码进行自动完成,但是下拉项目位于AutoCompleteTextView
. 我希望它显示在AutoCompleteTextView
. 我怎样才能做到这一点?
package com.example.autocomplete;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium","Belgam","Bellam", "France Belgium", "Italy", "Germany", "Spain"
};
}
xml:
xml:
<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="${relativePackage}.${activityClass}" >
<AutoCompleteTextView
android:id="@+id/countries_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:dropDownHeight="100dp"
android:ems="10"
android:text="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
采纳答案by Francesco verheye
Looks like this must work:
看起来这必须工作:
android:dropDownHeight="100dp"
Founded here: https://stackoverflow.com/a/7648028/1723525
在这里成立:https: //stackoverflow.com/a/7648028/1723525
Edit:
编辑:
I tried with a new Activity and the suggestions ara shown below. Maybe you have to change the dropDownHeight to 150dp because you have marginTop of 100dp. I guess that the dropDownHeight must be higher than the Y position (= padding + margin). Let me know if it works.
我尝试了一个新的 Activity 和下面显示的建议 ara。也许您必须将 dropDownHeight 更改为 150dp,因为您的 marginTop 为 100dp。我猜 dropDownHeight 必须高于 Y 位置(= 填充 + 边距)。让我知道它是否有效。
回答by Anh Duy
Set height of dropdown and set space between dropdown and edittext. Dropdown will display above and below view in dropdownanchor. Height = 200dp, dropdown will not overlap keyboard.
设置下拉的高度并设置下拉和编辑文本之间的空间。Dropdown 将在 dropdownanchor 中的视图上方和下方显示。高度 = 200dp,下拉菜单不会与键盘重叠。
<RelativeLayout
android:id="@+id/dropdownAutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingTop="8dp">
<AutoCompleteTextView
android:id="@+id/autoTextViewState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:digits="@string/get_started_petname_digits"
android:dropDownAnchor="@+id/dropdownAutoCompleteTextView"
android:dropDownHeight="200dp"
android:hint="Colorado"
android:imeOptions="actionNext"
android:inputType="textEmailAddress|textCapWords"
android:maxLength="15"
android:nextFocusDown="@+id/editZipCode"/>
</RelativeLayout>
回答by Dipen Dedania
Use the android:dropDownAnchor
, android:dropDownHorizontalOffset
, android:dropDownVerticalOffset
and maybe others on the AutoCompleteTextView
in your layout. For more info AutocompleteTextView.
在您的布局中使用android:dropDownAnchor
、android:dropDownHorizontalOffset
,android:dropDownVerticalOffset
也许还有其他人AutoCompleteTextView
。有关更多信息AutocompleteTextView。
回答by Kuldeep Sakhiya
Add dropdownheight as well as dropdownanchor, will work surely.
添加 dropdownheight 以及 dropdownanchor,肯定会起作用。
android:dropDownAnchor="@id/yourIdOfViewOnTop"
android:dropDownHeight="100dp"
回答by M?nh Hoàng Huynh
Let's understand how dropdown view will be rendered. In the fact, it's PopupWindowand it decide position (above or below compare with view archor) by method findDropDownPosition. Comment of this method said that:
让我们了解如何呈现下拉视图。实际上,它是PopupWindow,它通过findDropDownPosition方法决定位置(与视图 archor 相比的上方或下方)。这个方法的评论说:
Positions the popup window on screen. When the popup window is too tall to fit under the anchor, a parent scroll view is seeked and scrolled up to reclaim space. If scrolling is not possible or not enough, the popup window gets moved on top of the anchor. The results of positioning are placed in {@code outParams}.
在屏幕上定位弹出窗口。当弹出窗口太高而无法容纳在锚点下时,会寻找并向上滚动父滚动视图以回收空间。如果无法滚动或滚动不够,则弹出窗口将移动到锚点的顶部。定位结果放在{@code outParams}中。
I've search serveral days but no any solutions really work in case you want handle dropdown's position (my case is in DialogFragment). This is my experience after serveral days, you can (cheat) code to fix this problem:
我已经搜索了好几天,但没有任何解决方案真正有效,以防您想要处理下拉列表的位置(我的情况是在 DialogFragment 中)。这是我几天后的经验,你可以(作弊)代码来解决这个问题:
- Use
android:dropDownAnchor
to set anchor of dropdown to show, it's worked but UI is seem be too strange - Use
setDropDownVerticalOffset(offset: Int)
again, it's worked but again, UI is too strange - Set
android:dropDownHeight
to higher than height of (above or below) space, result will be the same as 2 ways above
- 使用
android:dropDownAnchor
于下拉的一组锚来展示,它的工作,但用户界面是显得太奇怪 setDropDownVerticalOffset(offset: Int)
再次使用,成功了,但又一次,UI 太奇怪了- 设置
android:dropDownHeight
为高于(上方或下方)空间的高度,结果将与上述 2 种方式相同