gpt4 book ai didi

android - AutoCompleteTextView - 如何删除顶部和底部的边距?

转载 作者:行者123 更新时间:2023-12-05 02:35:21 25 4
gpt4 key购买 nike

我正在使用 AutoCompleteTextView 创建下拉菜单。如何删除列表顶部和底部的默认边距?

enter image description here

重新创建:

<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" />

最佳答案


更新

All the credits and efforts back to @MikeM. from the comments. Many thanks to his efforts.

原因填充来自 TextInputLayout 本身,它的 DropdownMenuEndIconDelegate sets some MaterialShapeDrawables as the dropdown's background .垂直填充的值为 8dp,取自 this resource dimen .

解决方案一:

将此资源维度覆盖为 0dp:

<dimen name="mtrl_exposed_dropdown_menu_popup_vertical_padding" tools:override="true">0dp</dimen>

方案二:

之前的解决方案将应用于所有 AutoCompleteTextView,但如果您想对某些特定的执行此操作:更改默认的 dropDown 背景可绘制对象,例如:

autoCompleteTextView.setDropDownBackgroundDrawable(new ColorDrawable(Color.WHITE));
<罢工>

带有反射(不推荐,因为它是反模式)

AutoCompleteTextView 的下拉弹出窗口可以通过 mPopup field 在内部访问.通过反射获得它:

// Java:

public static ListPopupWindow getPopup(AutoCompleteTextView autoCompleteTextView) {
try {
Field field = AutoCompleteTextView.class.getDeclaredField("mPopup");
field.setAccessible(true);
return (ListPopupWindow) field.get(autoCompleteTextView);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}


// Kotlin:

fun AutoCompleteTextView.getPopup(): ListPopupWindow? {
try {
val field = AutoCompleteTextView::class.java.getDeclaredField("mPopup")
field.isAccessible = true
return field.get(this) as ListPopupWindow
} catch (e: NoSuchFieldException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
return null
}

然后通过将其设置为内部 ListView 的父 View 来移除填充:

// Java:

autoCompleteTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListPopupWindow popup = getPopup(autoCompleteTextView);
if (popup != null) {
ListView listView = popup.getListView();
if (listView!= null)
((View) listView.getParent()).setPadding(0, 0, 0, 0);
}
}
});

// Kotlin

autoCompleteTextView.setOnClickListener {
val popUp = autoCompleteTextView2.getPopup()?.listView?.parent
if (popUp != null)
(popUp as View).setPadding(0, 0, 0, 0)
}

这是在 OnClickListener 中完成的,因为当用户点击列表时,可空性检查将不匹配,因为列表已形成。

关于android - AutoCompleteTextView - 如何删除顶部和底部的边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70591091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com