Android----- 改变图标原有颜色 和 搜索框

本博客主要讲以下两点知识点

图标改变颜色:Drawable的变色,让Android也能有iOS那么方便的图片色调转换,就像同一个图标,但是有多个地方使用,并且颜色不一样,就可以用这个方法了。

**搜索框: **一般是EditText实现,本文 实现 TextView图片和文字居中,键盘搜索。

来看看效果图:

image

图标改变颜色:第一个界面的左边(二维码)和右边(更多)两个实现,我放进去的图片是黑色的,显示出来是白色的。

image

搜索框:第一个界面的图片和文字居中,还可以设置间距,第二个见面搜索设置键盘搜索按钮,点击搜索监听事件,清除内容的图标。

搜索框布局:

 <!--
            搜索图标设置  左边
            android:drawableLeft="@mipmap/icon_search"
            android:drawablePadding="5dp"  图标和文字的间距
            右边
            android:drawableRight="@mipmap/round_close"
            android:paddingRight="8dp"
            android:imeOptions="actionSearch"  设置成搜索按钮
        -->
       <EditText
            android:id="@+id/search_text"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="30dp"
            android:hint="输入要搜索的商品"
            android:background="@drawable/search_gray"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="9dp"
            android:textSize="12sp"
            android:drawableLeft="@mipmap/icon_search"
            android:paddingLeft="9dp"
            android:drawablePadding="5dp"
            android:drawableRight="@mipmap/round_close"
            android:paddingRight="8dp"
            android:imeOptions="actionSearch"
            android:maxLines="1"
            android:singleLine="true"
          />

键盘监听:

searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null) {
             //提示搜索内容
                    Toast.makeText(SearchActivity.this,searchText.getText().toString(),Toast.LENGTH_LONG).show();
                    //可以跳转搜索页面
                   /* Intent intent= new Intent(SearchActivity.this,SearchWebViewActivity.class);
                    intent.putExtra("model",model);
                    intent.putExtra("search",searchText.getText().toString());
                    startActivity(intent);
                    finish();*/
                }
                return false;
            }
        });

首页布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="45dp"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        >
        <ImageButton
            android:id="@+id/home_left_scan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="19dp"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:paddingLeft="11dp"
            android:layout_centerVertical="true"
            android:background="#00000000"
            />
        <com.zhangqie.searchbox.view.DrawableTextView
            android:id="@+id/home_search"
            android:layout_width="match_parent"
            android:layout_height="28dp"
            android:layout_weight="1"
            android:background="@drawable/search_view_background"
            android:gravity="center_vertical"
            android:maxLines="1"
            android:text="输入搜索相关内容"
            android:drawableLeft="@mipmap/icon_search"
            android:textSize="12sp"
            android:drawablePadding="11dp"
            />
        <ImageButton
            android:id="@+id/home_right_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:paddingRight="15dp"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:paddingLeft="15dp"
            android:background="#00000000"
            />
    </LinearLayout>

自定义DrawableTextView:(文字图标居中)

public class DrawableTextView extends TextView {

    public DrawableTextView(Context context, AttributeSet attrs,
                            int defStyle) {
        super(context, attrs, defStyle);
    }

    public DrawableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawableTextView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        // 得到drawableLeft设置的drawable对象
        Drawable leftDrawable = drawables[0];
        if (leftDrawable != null) {
            // 得到leftDrawable的宽度
            int leftDrawableWidth = leftDrawable.getIntrinsicWidth();
            // 得到drawable与text之间的间距
            int drawablePadding = getCompoundDrawablePadding();
            // 得到文本的宽度
            int textWidth = (int) getPaint().measureText(getText().toString().trim());
            int bodyWidth = leftDrawableWidth + drawablePadding + textWidth;
            canvas.save();
            canvas.translate((getWidth() - bodyWidth) / 2, 0);
        }
        super.onDraw(canvas);
    }
}

看似简单的效果,其实还是不简单的;加油吧! 有问题可以扫头像加新创建的群@我

源码下载

https://github.com/DickyQie/android-basic-control/tree/search-box

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,843评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,538评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,187评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,264评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,289评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,231评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,116评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,945评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,367评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,581评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,754评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,458评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,068评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,692评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,842评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,797评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,654评论 2 354

推荐阅读更多精彩内容