Android自定义SearchView布局调整:返回键、输入框、清除键排版异常
自定义SearchView实现返回键居左、输入框居中、清除键居右的布局方案
直接通过自定义布局替代系统默认SearchView结构,用ConstraintLayout实现精准排版,以下是完整示例代码:
1. 自定义SearchView布局文件 search_view.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingVertical="8dp" android:paddingHorizontal="16dp"> <!-- 返回按钮 --> <ImageView android:id="@+id/iv_back" android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/ic_back" android:scaleType="centerInside" android:clickable="true" android:focusable="true" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <!-- 搜索输入框 --> <EditText android:id="@+id/et_search" android:layout_width="0dp" android:layout_height="match_parent" android:hint="请输入搜索内容" android:background="@drawable/search_input_bg" android:paddingHorizontal="16dp" android:layout_marginHorizontal="16dp" app:layout_constraintLeft_toRightOf="@id/iv_back" app:layout_constraintRight_toLeftOf="@id/iv_clear" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <!-- 清除按钮 --> <ImageView android:id="@+id/iv_clear" android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/ic_clear" android:scaleType="centerInside" android:clickable="true" android:focusable="true" android:visibility="gone" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
说明:
- 用
ConstraintLayout的约束关系固定三个控件位置:返回键左贴父布局,清除键右贴父布局,输入框填充中间剩余空间 - 清除按钮默认隐藏,输入有内容时再显示
- 可自定义
@drawable/ic_back、@drawable/ic_clear、@drawable/search_input_bg的样式
2. 在AppBar中引用自定义布局(app_bar_main.xml)
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"> <!-- 替换系统SearchView为自定义布局 --> <include layout="@layout/search_view"/> </androidx.appcompat.widget.Toolbar>
3. MainActivity中的逻辑处理
public class MainActivity extends AppCompatActivity { private EditText etSearch; private ImageView ivBack, ivClear; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); // 隐藏默认标题 Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false); initView(); setListener(); } private void initView() { etSearch = findViewById(R.id.et_search); ivBack = findViewById(R.id.iv_back); ivClear = findViewById(R.id.iv_clear); } private void setListener() { // 返回按钮点击事件 ivBack.setOnClickListener(v -> onBackPressed()); // 清除按钮点击事件 ivClear.setOnClickListener(v -> { etSearch.setText(""); ivClear.setVisibility(View.GONE); }); // 监听输入内容变化,控制清除按钮显示/隐藏 etSearch.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { ivClear.setVisibility(s.length() > 0 ? View.VISIBLE : View.GONE); } @Override public void afterTextChanged(Editable s) {} }); } }
关键注意点
- 确保项目中引入了
androidx.constraintlayout:constraintlayout依赖 - 自定义输入框背景
search_input_bg可以是圆角矩形,模拟系统SearchView的样式 - 如果需要保留系统SearchView的搜索功能,可在
etSearch的onEditorAction中处理搜索提交逻辑
内容的提问来源于stack exchange,提问作者chris oojer
相关产品推荐
相关产品推荐

