调整 搜索图标的大小 样式 SearchView 网上好多都是说通过反射什么 一大堆的东西
我看了一下 挺麻烦的 我基础又不好 所以 只能去看源代码 研究一下 试一下
成功了 原来SearchView 的属性不受影响
有兴趣的 朋友们交流一下 如果帮到了你 记得点赞👍呦 😋
- 先看源码布局 然后找到我们需要用的控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- This is actually used for the badge icon *or* the badge label (or neither) -->
<TextView
android:id="@+id/search_badge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginBottom="2dip"
android:drawablePadding="0dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary"
android:visibility="gone" />
<ImageView
android:id="@+id/search_button"//输入框外的搜索按钮
style="?attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:focusable="true"
android:contentDescription="@string/abc_searchview_description_search" />
<LinearLayout
android:id="@+id/search_edit_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:orientation="horizontal"
android:layoutDirection="locale">
<ImageView
android:id="@+id/search_mag_icon"//输入框内的搜索按钮
android:layout_width="@dimen/abc_dropdownitem_icon_width"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_gravity="center_vertical"
android:visibility="gone"
style="@style/RtlOverlay.Widget.AppCompat.SearchView.MagIcon" />
<!-- 内部布局包含应用程序图标、按钮和EditText -->
<LinearLayout
android:id="@+id/search_plate"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
//输入框 默认高度36dp 设置 SearchView 高度的时候要注意 如果小于 这个高度 文字可能不上下居中显示
<view class="androidx.appcompat.widget.SearchView$SearchAutoComplete"
android:id="@+id/search_src_text"
android:layout_height="36dip"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:paddingLeft="@dimen/abc_dropdownitem_text_padding_left"
android:paddingRight="@dimen/abc_dropdownitem_text_padding_right"
android:singleLine="true"
android:ellipsize="end"
android:background="@null"
android:inputType="text|textAutoComplete|textNoSuggestions"
android:imeOptions="actionSearch"
android:dropDownHeight="wrap_content"
android:dropDownAnchor="@id/search_edit_frame"
android:dropDownVerticalOffset="0dip"
android:dropDownHorizontalOffset="0dip" />
<ImageView
android:id="@+id/search_close_btn"//删除按钮 设置了 paddingLeft paddingRight 设置宽度的时候注意一下
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:layout_gravity="center_vertical"
android:background="?attr/selectableItemBackgroundBorderless"
android:focusable="true"
android:contentDescription="@string/abc_searchview_description_clear" />
</LinearLayout>
<LinearLayout
android:id="@+id/submit_area"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/search_go_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:background="?attr/selectableItemBackgroundBorderless"
android:visibility="gone"
android:focusable="true"
android:contentDescription="@string/abc_searchview_description_submit" />
<ImageView
android:id="@+id/search_voice_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:background="?attr/selectableItemBackgroundBorderless"
android:visibility="gone"
android:focusable="true"
android:contentDescription="@string/abc_searchview_description_voice" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
- 控件找到了 我们还需要的定义我们需要的属性
<declare-styleable name="SelcetView">
<attr name="closeIcon" format="reference" />
<attr name="closeIconWidth" format="dimension" />
<attr name="closeIconHeight" format="dimension" />
<attr name="searchIcon" format="reference" />
<attr name="searchIconWidth" format="dimension" />
<attr name="searchIconHeight" format="dimension" />
<attr name="queryHintColor" format="color" />
<attr name="queryBackground" format="reference" />
<attr name="textSize" format="dimension" />
<attr name="textColor" format="color" />
<attr name="iconifiedByDefault" format="boolean" />
</declare-styleable>
- 属性定义好了 我们在代码中获取属性 然后在xml 设置你需要的属性 下面献上代码 若帮您解决了问题 记得点赞哟
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.ViewCompat;
import com.mylibrary.api.R;
import com.mylibrary.api.utils.SystemUtil;
public class SelcetView extends SearchView {
private Context context;
private boolean iconifiedByDefault;
private Drawable closeDrawable;
private Drawable searchDrawable;
private Drawable queryBackground;
private int closeWidth;
private int closeHeight;
private int searchWidth;
private int searchHeight;
private int queryHintColor;
private int textSize;
private int textColor;
private ImageView mCollapsedIcon;// iconifiedByDefault="false" 搜索按钮
private ImageView mCloseButton;//删除按钮
private ImageView mSearchButton;//iconifiedByDefault="true" 搜索按钮
private AppCompatAutoCompleteTextView searchText;//
private LinearLayout linearLayout;
private View mSearchPlate;//输入框的背景
public SelcetView(Context context) {
this(context, null);
}
public SelcetView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.searchViewStyle);
}
public SelcetView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
int defaultSize = SystemUtil.dp2px(context, 16);
mSearchButton = findViewById(R.id.search_button);
mCloseButton = findViewById(R.id.search_close_btn);
mCollapsedIcon = findViewById(R.id.search_mag_icon);
searchText = findViewById(R.id.search_src_text);
linearLayout = findViewById(R.id.search_edit_frame);
mSearchPlate = findViewById(R.id.search_plate);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SelcetView);
closeDrawable = array.getDrawable(R.styleable.SelcetView_closeIcon);
closeWidth = (int) array.getDimension(R.styleable.SelcetView_closeIconWidth, defaultSize);
closeHeight = (int) array.getDimension(R.styleable.SelcetView_closeIconHeight, defaultSize);
searchDrawable = array.getDrawable(R.styleable.SelcetView_searchIcon);
searchWidth = (int) array.getDimension(R.styleable.SelcetView_searchIconWidth, defaultSize);
searchHeight = (int) array.getDimension(R.styleable.SelcetView_searchIconHeight, defaultSize);
queryHintColor = array.getColor(R.styleable.SelcetView_queryHintColor, Color.parseColor("#8b8b8b"));
textColor = array.getColor(R.styleable.SelcetView_textColor, Color.parseColor("#333333"));
textSize = array.getColor(R.styleable.SelcetView_textSize, 12);
queryBackground = array.getDrawable(R.styleable.SelcetView_queryBackground);
iconifiedByDefault = array.getBoolean(R.styleable.SelcetView_iconifiedByDefault, false);
array.recycle();
if (linearLayout != null) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
params.leftMargin = SystemUtil.dp2px(context, 3);
}
setCloseSize(closeWidth, closeHeight);
setCloseDrawable(closeDrawable);
setSearchSize(searchWidth, searchHeight);
setSearchDrawable(searchDrawable);
setQueryHintColor(queryHintColor);
setTextColor(textColor);
setTextSize(textSize);
setIconifiedByDefault(iconifiedByDefault);
searchText.setFocusable(false);
if (queryBackground != null) {
ViewCompat.setBackground(mSearchPlate, queryBackground);
} else {
ViewCompat.setBackground(mSearchPlate, new ColorDrawable(0x00000000));
}
}
public void setCloseDrawable(int closeDrawable) {
setCloseDrawable(AppCompatResources.getDrawable(context, closeDrawable));
}
public void setSearchDrawable(int searchDrawable) {
setSearchDrawable(AppCompatResources.getDrawable(context, searchDrawable));
}
public void setCloseDrawable(Drawable closeDrawable) {
this.closeDrawable = closeDrawable;
if (closeDrawable != null && mCloseButton != null) {
closeDrawable.setBounds(0, 0, closeWidth, closeHeight);
mCloseButton.setImageDrawable(closeDrawable);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mCloseButton.getLayoutParams();
params.width = closeWidth;
params.height = closeWidth;
params.rightMargin = SystemUtil.dp2px(context, 3);
mCloseButton.setPadding(0, 0, 0, 0);
mCloseButton.setLayoutParams(params);
}
}
public void setSearchDrawable(Drawable searchDrawable) {
this.searchDrawable = searchDrawable;
if (searchDrawable != null) {
searchDrawable.setBounds(0, 0, searchWidth, searchHeight);
if (mSearchButton != null) {
mSearchButton.setImageDrawable(searchDrawable);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mSearchButton.getLayoutParams();
params.width = searchWidth;
params.height = searchHeight;
mSearchButton.setLayoutParams(params);
}
if (mCollapsedIcon != null) {
mCollapsedIcon.setImageDrawable(searchDrawable);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mCollapsedIcon.getLayoutParams();
params.width = searchWidth;
params.height = searchHeight;
mCollapsedIcon.setLayoutParams(params);
}
}
}
/**
* 设置大小 要在设置图标之前设置
**/
public void setCloseSize(int closeWidth, int closeHeight) {
this.closeWidth = closeWidth;
this.closeHeight = closeHeight;
}
/**
* 设置大小 要在设置图标之前设置
**/
public void setSearchSize(int searchWidth, int searchHeight) {
this.searchWidth = searchWidth;
this.searchHeight = searchHeight;
}
public void setQueryHintColor(int queryHintColor) {
this.queryHintColor = queryHintColor;
if (searchText != null) {
searchText.setHintTextColor(queryHintColor);
}
}
public void setTextSize(int textSize) {
this.textSize = textSize;
if (searchText != null) {
searchText.setTextSize(textSize);
}
}
public void setTextColor(int textColor) {
this.textColor = textColor;
if (searchText != null) {
searchText.setTextColor(textColor);
}
}
public void setListener(OnClickListener listener) {
if (listener != null) {
searchText.setOnClickListener(listener);
mCollapsedIcon.setOnClickListener(listener);
}
}
public TextView getEdittext() {
return searchText;
}
public void setEditFocusable(boolean focusable) {
searchText.setFocusable(focusable);
searchText.setFocusableInTouchMode(focusable);
searchText.requestFocus();
}
/**
* 搜索框内 搜索图标监听
**/
public void setSearchCollapsedListener(OnClickListener listener) {
if (listener != null && mCollapsedIcon != null) {
mCollapsedIcon.setOnClickListener(listener);
}
}
}