一、整体布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:background="@drawable/search_box_bg">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:src="@mipmap/cp_ic_search"
android:scaleType="center"
tools:ignore="ContentDescription" />
<EditText
android:id="@+id/et_search"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@null"
android:gravity="center_vertical"
android:hint="@string/cp_hint_search_box"
android:textColorHint="@color/cp_gray"
android:inputType="text"
android:singleLine="true"
android:textColor="@color/cp_gray"
android:textSize="14sp"
tools:ignore="RtlHardcoded" />
<ImageView
android:id="@+id/iv_search_clear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:src="@mipmap/cp_ic_search_clear"
android:visibility="gone"
tools:ignore="ContentDescription" />
</LinearLayout>
二、相关资源
drawable/search_box_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="6dp"/>
<solid android:color="@android:color/white"/>
</shape>
colors.xml
<color name="cp_gray">#8c8c8c</color>
strings.xml
<string name="cp_hint_search_box">请输入城市名或拼音</string>
<string name="cp_hot_city">热门城市</string>
<string name="cp_located_city">定位城市</string>
<string name="cp_located_failed">定位失败</string>
<string name="cp_locating">正在定位…</string>
<string name="cp_nothing_found">没有搜索结果~</string>
<string name="cp_select_city">选择城市</string>
图片