简体   繁体   English

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

[英]AutoCompleteTextView - How to remove margin from top and bottom?

I'm creating a dropdown menu using AutoCompleteTextView .我正在使用AutoCompleteTextView创建一个下拉菜单。 How do I remove the default margins at the top and bottom of the list?如何删除列表顶部和底部的默认边距?

在此处输入图像描述

To recreate:要重新创建:

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

Try removing dropDownHeight attribute from the AutoCompleteTextView尝试从 AutoCompleteTextView 中删除dropDownHeight属性

android:dropDownHeight="200dp" --> remove this line

And also set your AutoCompleteTextView height as wrap_content .并将您的 AutoCompleteTextView 高度设置为wrap_content

Meanwhile I don't think there is a direct API that allows you to do that;同时,我认为没有直接的 API 可以让您这样做; but you can achieve it with reflections as a last resort.但是您可以通过反射作为最后的手段来实现它。 Hopefully there would be an API, as reflections is anti-pattern.希望会有一个 API,因为反射是反模式。

The drop down popup of the AutoCompleteTextView can be accessed internally by mPopup field . AutoCompleteTextView的下拉弹出窗口可以通过mPopup field内部访问。 To get it with reflections:要通过反射获得它:

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

Then remove the padding by setting it to the parent view of the internal ListView :然后通过将其设置为内部ListView的父视图来删除填充:

// 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)
}

This is done in OnClickListener because the nullability checks won't match as the list is formed when the user hits it.这是在OnClickListener中完成的,因为当用户点击列表时,可空性检查将不匹配。

It won't let me edit the answer but another solution by https://stackoverflow.com/users/2850651/mike-m to remove padding:它不会让我编辑答案,而是https://stackoverflow.com/users/2850651/mike-m的另一个解决方案来删除填充:

Calling .showDropDown() in autoCompleteTextView 's .setOnClickListener()autoCompleteTextView.showDropDown()中调用 .showDropDown .setOnClickListener()

<com.google.android.material.textfield.MaterialAutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:drawableEnd="@drawable/ic_arrow_drop_down"
        android:dropDownHeight="200dp"
        android:inputType="none"
        android:text="Select age" 
        ...../>

List<String> dropdownItems = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
     dropdownItems.add(String.valueOf(i));
}

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(
    this, R.layout.dropdown_item, dropdownItems
);


MaterialAutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
autoCompleteTextView.setAdapter(spinnerAdapter);

//.showDropDown() removes the padding
autoCompleteTextView.setOnClickListener(v -> autoCompleteTextView.showDropDown());

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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