美文网首页程序员
Android SearchView 调整 搜索图标的大小 样式

Android SearchView 调整 搜索图标的大小 样式

作者: 天堂守望者 | 来源:发表于2019-12-03 13:24 被阅读0次

调整 搜索图标的大小 样式 SearchView 网上好多都是说通过反射什么 一大堆的东西
我看了一下 挺麻烦的 我基础又不好 所以 只能去看源代码 研究一下 试一下
成功了 原来SearchView 的属性不受影响
有兴趣的 朋友们交流一下 如果帮到了你 记得点赞👍呦 😋


searchview.jpg
  1. 先看源码布局 然后找到我们需要用的控件
<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>
  1. 控件找到了 我们还需要的定义我们需要的属性
 <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>
  1. 属性定义好了 我们在代码中获取属性 然后在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);
        }
    }
}

相关文章

  • Android SearchView 调整 搜索图标的大小 样式

    调整 搜索图标的大小 样式 SearchView 网上好多都是说通过反射什么 一大堆的东西我看了一下 挺麻烦的 ...

  • 搜索框(searchView)使用

    目录 searchView searchView是搜索框.提供搜索框的图形界面. 使用方式: searchView...

  • SearchView

    Android 搜索框:SearchView 的属性和用法详解MaterialDesign学习篇(五),使用Sea...

  • Android搜索控件SearchView

    由于项目很多地方需要搜索框,就自定义了一个SearchView控件,顺便复习下自定义View的操作。 一.复用性 ...

  • MaterialSearchView搜索

    Android Material Design 中其实有搜索框SearchView,但是并不怎么好用,所以这里介绍...

  • MaterialSearchView搜索框

    Android Material Design 中其实有搜索框SearchView,但是并不怎么好用,所以这里介绍...

  • MaterialSearchView搜索框

    Android Material Design 中其实有搜索框SearchView,但是并不怎么好用,所以这里介绍...

  • 近期遇到的问题

    matlab 绘图时总是在调整图的大小、字体大小及样式上面画很多时间,有没有高效的办法绘制合适大小的图,插入到Wo...

  • SearchView的使用

    搜索功能是Android常见的功能之一,在v7包就有一个控件SearchView,用来实现搜索功能,而在普通wid...

  • 搜索框----对一个第三方库使用的简单介绍

    最近在项目开发的过程中涉及到搜索框的内容,其实android本身已经提供了一个SearchView用来做搜索功能,...

网友评论

    本文标题:Android SearchView 调整 搜索图标的大小 样式

    本文链接:https://www.haomeiwen.com/subject/exoqgctx.html