Android----- 改變表徵圖原有顏色 和 搜尋方塊

來源:互聯網
上載者:User

標籤:點擊   sla   top   save   style   attrs   cte   --   []   

本部落客要講以下兩點知識點

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

搜尋方塊: 一般是EditText實現,本文 實現 TextView圖片和文字置中,鍵盤搜尋。

來看看:

    

 

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

           

搜尋方塊:第一個介面的圖片和文字置中,還可以設定間距,第二個見面搜尋設定鍵盤搜尋按鈕,點擊搜尋監聽事件,清除內容的表徵圖。

搜尋方塊布局:

 

    <!--            搜尋表徵圖設定  左邊            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);    }}

 

 

 

  看似簡單的效果,其實還是不簡單的;加油吧!  有問題可以掃頭像加新建立的群@我 

 

  源碼下載

 

Android----- 改變表徵圖原有顏色 和 搜尋方塊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.