国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 改變圖標原有顏色和搜索框的實例代碼

2019-10-22 18:27:34
字體:
來源:轉載
供稿:網友

圖標改變顏色:Drawable的變色,讓Android也能有iOS那么方便的圖片色調轉換,就像同一個圖標,但是有多個地方使用,并且顏色不一樣,就可以用這個方法了。

搜索框: 一般是EditText實現,本文 實現 TextView圖片和文字居中,鍵盤搜索。

來看看效果圖:

android,圖標顏色,搜索

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

android,圖標顏色,搜索

搜索框:第一個界面的圖片和文字居中,還可以設置間距,第二個見面搜索設置鍵盤搜索按鈕,點擊搜索監聽事件,清除內容的圖標。

搜索框布局:

<!--    搜索圖標設置 左邊    android/196745.html">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

總結

以上所述是小編給大家介紹的Android 改變圖標原有顏色和搜索框的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临西县| 吴忠市| 潮州市| 章丘市| 仙桃市| 怀仁县| 德兴市| 长垣县| 东兴市| 岑巩县| 吉水县| 台北市| 尖扎县| 高平市| 桐乡市| 九龙坡区| 萝北县| 宜阳县| 通城县| 保靖县| 平乡县| 察哈| 华容县| 怀化市| 亚东县| 冀州市| 纳雍县| 黑河市| 茂名市| 淮滨县| 高雄县| 双牌县| 长沙县| 宜宾县| 商都县| 甘孜县| 绍兴市| 乐昌市| 高唐县| 上饶县| 赤水市|