Android----- 改變圖標(biāo)原有顏色 和 搜索框

本博客主要講以下兩點(diǎn)知識(shí)點(diǎn)

圖標(biāo)改變顏色:Drawable的變色,讓Android也能有iOS那么方便的圖片色調(diào)轉(zhuǎn)換螃壤,就像同一個(gè)圖標(biāo)描扯,但是有多個(gè)地方使用,并且顏色不一樣踢械,就可以用這個(gè)方法了酗电。

**搜索框: **一般是EditText實(shí)現(xiàn),本文 實(shí)現(xiàn) TextView圖片和文字居中内列,鍵盤搜索顾瞻。

來看看效果圖:

image

圖標(biāo)改變顏色:第一個(gè)界面的左邊(二維碼)和右邊(更多)兩個(gè)實(shí)現(xiàn),我放進(jìn)去的圖片是黑色的德绿,顯示出來是白色的荷荤。

image

搜索框:第一個(gè)界面的圖片和文字居中,還可以設(shè)置間距移稳,第二個(gè)見面搜索設(shè)置鍵盤搜索按鈕蕴纳,點(diǎn)擊搜索監(jiān)聽事件,清除內(nèi)容的圖標(biāo)个粱。

搜索框布局:

 <!--
            搜索圖標(biāo)設(shè)置  左邊
            android:drawableLeft="@mipmap/icon_search"
            android:drawablePadding="5dp"  圖標(biāo)和文字的間距
            右邊
            android:drawableRight="@mipmap/round_close"
            android:paddingRight="8dp"
            android:imeOptions="actionSearch"  設(shè)置成搜索按鈕
        -->
       <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"
          />

鍵盤監(jiān)聽:

searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null) {
             //提示搜索內(nèi)容
                    Toast.makeText(SearchActivity.this,searchText.getText().toString(),Toast.LENGTH_LONG).show();
                    //可以跳轉(zhuǎn)搜索頁面
                   /* 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="輸入搜索相關(guān)內(nèi)容"
            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:(文字圖標(biāo)居中)

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設(shè)置的drawable對(duì)象
        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);
    }
}

看似簡單的效果古毛,其實(shí)還是不簡單的;加油吧都许! 有問題可以掃頭像加新創(chuàng)建的群@我

源碼下載

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末稻薇,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子胶征,更是在濱河造成了極大的恐慌塞椎,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件睛低,死亡現(xiàn)場離奇詭異案狠,居然都是意外死亡服傍,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門骂铁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吹零,“玉大人,你說我怎么就攤上這事拉庵〔右危” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵钞支,是天一觀的道長阱扬。 經(jīng)常有香客問我,道長伸辟,這世上最難降的妖魔是什么麻惶? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮信夫,結(jié)果婚禮上窃蹋,老公的妹妹穿的比我還像新娘。我一直安慰自己静稻,他們只是感情好警没,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著振湾,像睡著了一般杀迹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上押搪,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天树酪,我揣著相機(jī)與錄音,去河邊找鬼大州。 笑死续语,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的厦画。 我是一名探鬼主播疮茄,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼根暑!你這毒婦竟也來了力试?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤排嫌,失蹤者是張志新(化名)和其女友劉穎畸裳,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體躏率,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡躯畴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年民鼓,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了薇芝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蓬抄。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖夯到,靈堂內(nèi)的尸體忽然破棺而出嚷缭,到底是詐尸還是另有隱情,我是刑警寧澤耍贾,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布阅爽,位于F島的核電站,受9級(jí)特大地震影響荐开,放射性物質(zhì)發(fā)生泄漏付翁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一晃听、第九天 我趴在偏房一處隱蔽的房頂上張望百侧。 院中可真熱鬧,春花似錦能扒、人聲如沸佣渴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽辛润。三九已至,卻和暖如春见秤,著一層夾襖步出監(jiān)牢的瞬間砂竖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國打工鹃答, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留晦溪,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓挣跋,卻偏偏與公主長得像三圆,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子避咆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容