【问题标题】:Xamarin.android Open Autocomplete Dropdown UpwardsXamarin.android 向上打开自动完成下拉菜单
【发布时间】:2021-07-01 14:48:21
【问题描述】:

我有一个自动完成下拉菜单,它会打开,不仅与文本字段重叠,而且还位于键盘后面。我需要下拉菜单来说明打开时的文本字段和键盘,而不是向下打开键盘和文本字段。由于建议与文本字段重叠,用户无法看到他们正在输入的内容。下面是我的代码

包含自动完成的布局是 selectionList.xml:

    <LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:orientation="vertical"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="match_parent"
    p1:layout_height="wrap_content"
    p1:id="@+id/mainLayout">
    <RelativeLayout
        p1:orientation="horizontal"
        p1:minWidth="25px"
        p1:minHeight="25px"
        p1:id="@+id/mainLayout2"
        p1:background="@drawable/border_full_white_black"
        p1:layout_width="match_parent"
        p1:layout_height="52dp"
        p1:layout_marginLeft="5dp"
        p1:layout_marginTop="2dp"
        p1:layout_marginRight="5dp"
        p1:layout_marginBottom="2dp">
        <TextView
            p1:layout_width="100dp"
            p1:layout_height="match_parent"
            p1:id="@+id/label"
            p1:textColor="#47463f"
            p1:gravity="center"
            p1:layout_marginRight="5dp"
            p1:layout_marginLeft="5dp"
            p1:textAppearance="?android:attr/textAppearanceMedium"
            p1:layout_alignParentLeft="true" />
        <TextView xmlns:p2="http://schemas.android.com/tools"
            p1:textAppearance="?android:attr/textAppearanceSmall"
            p1:layout_width="20dp"
            p1:layout_height="wrap_content"
            p1:id="@+id/required_label"
            p1:text="*"
            p1:textColor="#F0b323"
            p1:gravity="center"
            p1:textSize="20dp"
            p1:layout_alignParentRight="true"
            p1:layout_marginRight="5dp" />
        <EditText xmlns:tools="http://schemas.android.com/tools"
            p1:layout_width="match_parent"
            p1:layout_height="match_parent"
            p1:id="@+id/manual_edittext"
            p1:gravity="center"
            p1:textColor="#47463f"
            p1:layout_marginTop="2dp"
            p1:layout_marginRight="5dp"
            p1:layout_marginBottom="5dp"
            p1:background="@drawable/backgroundstyle"
            tools:visibility="invisible"
            p1:layout_toLeftOf="@+id/required_label"
            p1:layout_toEndOf="@+id/user_label" />
        <AutoCompleteTextView
            p1:id="@+id/autoComplete"
            p1:visibility="invisible"
            p1:gravity="center"
            p1:layout_width="match_parent"
            p1:layout_height="wrap_content"
            p1:layout_marginTop="2dp"
            p1:layout_marginRight="2dp"
            p1:layout_marginBottom="5dp"
            p1:singleLine="true"
            p1:imeOptions="actionDone"
            p1:popupBackground="#ffffff"
            p1:maxLength="2"
            p1:dropDownAnchor="@+id/autoComplete"
            p1:dropDownHeight="wrap_content"
            p1:background="@drawable/border_bottom_lightgray_white"
            p1:layout_toLeftOf="@+id/required_label"
            p1:layout_toEndOf="@+id/label" 
            p1:textColor="#47463f"/>
    </RelativeLayout>
</LinearLayout>

自动完成布局资源文件为autocomplete_layout.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:p1="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
p1:background="@android:color/white"
p1:layout_width="fill_parent"
p1:layout_height="wrap_content">
 <TextView
    p1:text="Medium Text"
     p1:textAppearance="?android:attr/textAppearanceMedium"
     p1:layout_width="match_parent"
     p1:layout_height="wrap_content"
     p1:id="@+id/textView1"
     p1:textColor="#ff000000"
     p1:gravity="center"
     p1:paddingBottom="8dip"
     p1:paddingTop="8dip"
     p1:background="@drawable/border_bottom_lightgray_white"
     p1:textSize="15dp" />
 </LinearLayout>

设置适配器的代码是:

   var adapter = new ArrayAdapter<String>(this, Resource.Layout.autocomplete_layout, Resource.Id.textView1, list);

目前下拉菜单重叠如下。

【问题讨论】:

    标签: android xml xamarin xamarin.android autocompletetextview


    【解决方案1】:

    根据你的描述和你的代码,我猜你想为 Xamarin.Android 创建自动完成,如果是,你可以看看下面的代码。

    首先,创建list_item.xml 并将其保存在 Resources/Layout 文件夹中,将用于建议列表中出现的每个项目。

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#000">
    </TextView> 
    

    然后将内容视图设置为 main.xml 布局。

     public class MainActivity : AppCompatActivity
    {
        static readonly string[] countries = new String[] {
    "Afghanistan","Albania","Algeria","American Samoa","Andorra",   
    "Vanuatu","Vatican City","Venezuela","Vietnam","Wallis and Futuna","Western Sahara",
    "Yemen","Yugoslavia","Zambia","Zimbabwe"
    };
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
    
            AutoCompleteTextView textView = FindViewById<AutoCompleteTextView>(Resource.Id.autocomplete_country);
            var adapter = new ArrayAdapter<String>(this, Resource.Layout.list_item, countries);
    
            textView.Adapter = adapter;
        }
      
    }
    

    Main.xaml。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Country" />
    <AutoCompleteTextView android:id="@+id/autocomplete_country"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"/>
    

    更详细的信息,你可以看看:

    Auto Complete for Xamarin.Android

    截图:

    【讨论】:

    • 嗨,我已经有自动完成设置,但需要它向上打开,因为我有一个下拉列表,屏幕底部的下拉列表似乎打开了与键盘和输入字段。我不希望它包裹在键盘上。相反,它应该向上打开并且不与搜索文本或键盘重叠。
    • @z123 能否在gtihub提供一个简单的示例,我会下载您的示例进行测试,有助于理解您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多