根据您的需要尝试此操作,但不要考虑使用您自己的按钮,您可以通过提供 wrap_content 宽度参数将默认 searchButton 移动到右侧,然后当您单击搜索时将宽度设置为 match_parent,然后在关闭它时将宽度设置为 wrap_content
使用 RelativeLayout 的 searchView 子项,并确保正确的 LayoutParams(RelativeLayout.LayoutParams)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/counter_text_color" >
<Button
android:id="@+id/buttonLeftMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:background="@drawable/ic_drawer" />
<ImageView
android:id="@+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/actionbar_logo" />
<TextView
android:id="@+id/tw_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="YOUR "
android:textSize="24sp" />
<SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/buttonRightMenu" />
<Button
android:id="@+id/buttonRightMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="@drawable/ic_action_settings" />
</RelativeLayout>
在你的课堂上
searchView.setOnSearchClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, R.id.buttonLeftMenu);
params.addRule(RelativeLayout.LEFT_OF, R.id.buttonRightMenu);
searchView.setLayoutParams(params);
}
});
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF, R.id.buttonRightMenu);
searchView.setLayoutParams(params);
return false;
}
});