Android 改變圖標(biāo)原有顏色和搜索框的實例代碼
圖標(biāo)改變顏色:Drawable的變色,讓Android也能有iOS那么方便的圖片色調(diào)轉(zhuǎn)換,就像同一個圖標(biāo),但是有多個地方使用,并且顏色不一樣,就可以用這個方法了。
搜索框: 一般是EditText實現(xiàn),本文 實現(xiàn) TextView圖片和文字居中,鍵盤搜索。
來看看效果圖:

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

搜索框:第一個界面的圖片和文字居中,還可以設(shè)置間距,第二個見面搜索設(shè)置鍵盤搜索按鈕,點擊搜索監(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對象
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
總結(jié)
以上所述是小編給大家介紹的Android 改變圖標(biāo)原有顏色和搜索框的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android如何動態(tài)改變App桌面圖標(biāo)
- Android實現(xiàn)動態(tài)改變app圖標(biāo)的示例代碼
- Android實現(xiàn)修改狀態(tài)欄背景、字體和圖標(biāo)顏色的方法
- Android 修改app圖標(biāo)和名稱的方法
- Android App更改應(yīng)用的圖標(biāo)的實現(xiàn)方法
- android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例
- Android改變ExpandableListView的indicator圖標(biāo)實現(xiàn)方法
- Android動態(tài)修改應(yīng)用圖標(biāo)與名稱的方法實例
相關(guān)文章
基于Android實現(xiàn)保存圖片到本地并可以在相冊中顯示出來
App應(yīng)用越來越人性化,不僅界面優(yōu)美而且服務(wù)也很多樣化,操作也非常方便。通過本篇文章給大家介紹基于Android實現(xiàn)保存圖片到本地并可以在相冊中顯示出來,對android保存圖片相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Android Studio Gradle 更換阿里云鏡像的方法
這篇文章主要介紹了Android Studio Gradle 更換阿里云鏡像的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Android嵌套滾動NestedScroll的實現(xiàn)了解一下
嵌套滾動已經(jīng)算一個比較常見的特效了,這篇文章主要介紹了Android嵌套滾動NestedScroll的實現(xiàn)了解一下,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Android實現(xiàn)網(wǎng)易Tab分類排序控件實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易Tab分類排序控件的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android編程學(xué)習(xí)之抽象類AbsListView用法實例分析
這篇文章主要介紹了Android編程學(xué)習(xí)之抽象類AbsListView用法,較為詳細(xì)的分析了抽象類AbsListView的功能、結(jié)構(gòu)、定義及使用注意事項等,需要的朋友可以參考下2015-10-10

