You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Android:如何将EditText放置在ListView上方?

解决EditText与ListView并排的布局问题

问题的核心原因很明确:你的根布局LinearLayout设置了android:orientation="horizontal",这个属性会强制子控件横向并排排列,所以SearchBox(EditText)和ListView才会挤在同一行。要实现EditText在ListView上方的垂直布局,只需要做两个关键调整:

  1. 把LinearLayout的orientation属性从horizontal改为vertical
  2. 调整EditText的宽度为match_parent,让它铺满屏幕宽度(原wrap_content会让搜索框只适配内容宽度,交互体验不佳)

修改后的完整布局代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" <!-- 改为垂直方向 -->
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="#edf0f4">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent" <!-- 铺满屏幕宽度 -->
        android:layout_height="40dp" <!-- 适当调高高度,提升点击体验 -->
        android:background="#d0d4db"
        android:layout_marginLeft="15px"
        android:layout_marginRight="15px"
        android:layout_marginTop="15px"
        android:hint="Search"
        android:textColorHint="#FFFFFF"
        android:textColor="#000000"
        android:paddingHorizontal="10dp" <!-- 添加内边距,避免文字贴边 -->
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_marginTop="15px" <!-- 顶部间距,和搜索框分隔开 -->
        android:layout_marginHorizontal="15px" <!-- 可选:左右间距与搜索框对齐 -->
        />
</LinearLayout>

额外补充几个优化细节:

  • 将EditText高度从25dp调整为40dp,扩大点击区域,提升用户交互体验
  • 给EditText添加水平内边距,避免输入文字紧贴边框
  • 给ListView增加顶部和左右间距,让整体布局更协调美观

内容的提问来源于stack exchange,提问作者user9198450

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:26:05