【发布时间】:2022-01-05 10:11:04
【问题描述】:
我正在使用AutoCompleteTextView 创建一个下拉菜单。如何删除列表顶部和底部的默认边距?
重新创建:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:boxBackgroundColor="@color/white"
android:focusable="false"
app:boxStrokeWidth="1.5dp"
app:layout_constraintTop_toBottomOf="@+id/spinner_network">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="250dp"
android:layout_height="match_parent"
android:inputType="none"
android:dropDownHeight="200dp"
android:text="Select age" />
</com.google.android.material.textfield.TextInputLayout>
List<String> dropdownItems = new ArrayList<>();
for(int i = 1; i <= 100; i++){
dropdownItems.add(String.valueOf(i));
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(
this, R.layout.dropdown_item, dropdownItems
);
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
autoCompleteTextView.setAdapter(spinnerAdapter);
R.layout.dropdown_item
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="match_parent"
android:textColor="@color/black"
android:padding="14dp"
android:layout_height="wrap_content"
android:text="TextView" />
【问题讨论】:
-
好的,是的,我最初并没有意识到你在做什么;您并没有真正将其用作
AutoCompleteTextView。好吧,在这里忽略我以前的 cmets,因为我认为这只是针对常规的AutoCompleteTextView设置。如果我想到别的,我会告诉你的。顺便说一句,您的 XML 或代码确实没有问题。 ACTV 正在按预期工作。 TIL 只是添加了一些花里胡哨,其中之一就是可点击的下拉箭头。 ACTV 自己没有。 -
我发现在 ACTV 上设置
OnClickListener确实有效,因为它不接受文本输入。这意味着我们基本上可以在没有 TIL 的情况下复制 TIL 的行为:drive.google.com/file/d/10fpJswrcJ7MiDnRJH7Ycv49p3J37f2nO/…。我无法在这台机器 atm 上获得流畅的视频,但它可以工作:i.stack.imgur.com/T2QrH.png。不确定你是否真的想要边框,但你可以看到填充消失了(所以它一定来自某个地方的 TIL,但我还没有找到它)。仅供参考,如果对你有帮助的话。 -
@MikeM。绝对的传奇。它修复了边距问题,在没有 TIL 的情况下工作(我不喜欢闪烁的下拉动画),现在我可以使用这个解决方案而不是微调器来实现简单的 dropDownHeight。谢谢一堆。发表你的答案并声称你的赏金老板是你应得的 Lol
-
没问题。不过,我不再在这里发布答案,所以请随意完成这个,但你喜欢。我仍然想追踪填充的来源,如果我想出某种方法从 XML 中关闭它,我会告诉你的。不过谢谢。我很感激这个提议。很高兴我们发现了一些有用的东西。干杯!
-
我找到了它的来源,并在下面对 Zain 的回答添加了一些注释,因为他们首先确认父母的填充导致了这些差距。
标签: android material-design autocompletetextview material-components-android android-textinputlayout