将下拉列表作为布局中的视图,如 relativelayout 或其他视图

making the dropdown list as a view in the layout like the relativelayout or anothor view

我有 AutoCompleteTextView,它与下拉列表配合得很好。 我想将下拉列表显示为 AutoCompleteTextView 下布局的一部分,而不是布局上方。 这是布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@color/year_tablayout"
        android:layout_below="@+id/top_menu"
        android:background="@color/year_tablayout"
        app:tabIndicator="@null"
        app:tabMode="scrollable">
    </com.google.android.material.tabs.TabLayout>
    <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/autocomplete_city"
        android:layout_width="match_parent"
        android:background="@null"
        android:hint="Search Here ..."
        android:completionThreshold="0"
        android:textColorHint="#aaa"
        android:paddingLeft="@dimen/_10sdp"
        android:textCursorDrawable="@drawable/cursor"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_search_black_24dp"
        android:paddingRight="10dp"/>
    <com.proj.www.ui.mainpage.NonSwipeableViewPager
        android:id="@+id/viewPager_city"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/top_menu"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@color/tumblr_white"
        />
</LinearLayout>

这是片段代码

  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.city_fragment, container, false);
        AutoCompleteTextView autoCity = view.findViewById(R.id.autocomplete_city);
        String[] city = getResources().getStringArray(R.array.city_array);
        ArrayAdapter<String> autoAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, city);
        autoCity.setAdapter(autoAdapter);
}}

我想将下拉列表作为属于布局的视图并显示在 AutoCompleteTextView 下。 提前致谢。

希望明白你的意思:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.kt

import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val savedemail = listOf("red", "blue", "green", "yellow")
        val adapter = ArrayAdapter<String>(
            this, // Context
            android.R.layout.simple_dropdown_item_1line, // Layout
            savedemail // Array
        )
        text.setAdapter(adapter)
        text.threshold = 1
        text.onFocusChangeListener = View.OnFocusChangeListener { view, b ->
            if (b) {
                text.showDropDown()
            }
        }
    }
}