微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin.android 向上打开自动完成下拉菜单

如何解决Xamarin.android 向上打开自动完成下拉菜单

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

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

$ curl http://localhost:3000/handler
Hello
$ curl -X POST http://localhost:3000/change-value?value=SecondMessage
$ curl http://localhost:3000/handler
SecondMessage

自动完成布局资源文件是autocomplete_layout.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>

设置适配器的代码是:

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

目前下拉菜单重叠如下。

enter image description here

解决方法

根据你的描述和你的代码,我猜你想为 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

截图:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。