android button 基本原理

2021-06-08 04:56:26 字數 3027 閱讀 9807

2009-12-01 15:38:02

|  分類:

android相關|字型大小

訂閱

在android中button是非常常用的乙個view控制項,原本以為button實現的**肯定很多,但是看了原來著實吃了一驚.button的原始碼幾乎僅僅對繼承的textview類做了乙個小小的修改,僅僅是加了乙個style. 乙個style就能夠實現button的顯示效果樣式麼?android的style機制真的很強大.

首先來看一下buttonview的實現**:

*xml attributes

* see ,  

* ,   

*  * 

*/ ublic class button extends textview  

public button(context context, attributeset attrs)  

public button(context context, attributeset attrs, int defstyle)  

可以看到,button繼承了textview之後,僅僅是新增了乙個預設的style —

com.android.internal.r.attr.buttonstyle
我們知道,button其實在textview的基礎之上增加了按鈕的背景效果以及按鈕按下去的press效果。這麼乙個style檔案可以搞定這件事情麼?順著這個style找下去,在android的原始碼中找到style.xml,並找到相關的定義:

這裡定義了好多style相關的屬性,其他的屬性都好理解,這個backgroud屬性難道僅僅是乙個drawable?如果僅僅是乙個的化,怎麼能夠實現button各種狀態下表現出不同背景的功能呢?還是來看看這個drawable到底是什麼東西。

到drwable目錄中發現這個btn_default原來也是乙個xml檔案,內容如下:

?    android:drawable="@drawable/btn_default_normal" /> 

?    android:drawable="@drawable/btn_default_normal_disable" /> 

android:drawable="@drawable/btn_default_pressed" /> 

android:drawable="@drawable/btn_default_selected" /> 

android:drawable="@drawable/btn_default_normal" /> 

android:drawable="@drawable/btn_default_normal_disable_focused" /> 

android:drawable="@drawable/btn_default_normal_disable" /> 

其實drawable在android中有很多種,最普通的就是乙個。而這裡用到的是statelistdrawable。當android的解析器解析到上面的xml時,會自動轉化成乙個statelistdrawable類的例項。這個類的一些核心**如下:

public class statelistdrawable extends drawablecontainer  

/** 

* add a new image/string id to the set of images. 

* @param stateset - an array of resource ids to associate with the image. 

* switch to this image by calling setstate().  

* @param drawable -the image to show. 

*/ public void addstate(int stateset, drawable drawable)  

} ... 

}

xml裡面每乙個item就對應一種狀態,而每乙個有state_的屬性就是乙個狀態的描述,drawable則是真正的drawable(這個其實也可以是其他型別的drawable例項)。

各種view的狀態列表:

/** 

* the order here is very important to  

*/ private static final int view_state_sets = ; 

設定background的**:

/** 

* set the background to a given drawable, or remove the background. if the 

* background has padding, this view's padding is set to the background's 

* padding. however, when a background is removed, this view's padding isn't 

* touched. if setting the padding is desired, please use 

* . 

*  * @param d the drawable to use as the background, or null to remove the 

*        background 

*/ public void setbackgrounddrawable(drawable d) 

d.setvisible(getvisibility() == visible, false); 

mbgdrawable = d; 

... 

invalidate(); 

}

紅色的部分就是首先判斷這個drawable物件是否支援state切換,當然我們這裡的drawable是支援的。然後設定狀態,達到切換的效果。

所以,以後作一些需要根據狀態切換不同的效果可以用這個方法啦。。。

android button背景隨心搭配

裡我舉例如何使用selector,layer list完成button背景的層疊組合,從而有不同情形下不同效果 button ctrl.xml中選擇器 xml version 1.0 encoding utf 8 selector xmlns android item android state p...

Android Button實現功能的三種方法

android button實現功能的三種方法 1.一次性 使用的button,一般採用匿名內部類 button button button findviewbyid r.id.button button.setonclicklistener new onclicklistener 該方法適用於bu...

Androidbutton事件的五中寫法總結

button事件的五中寫法 1 匿名內部類 2 類實現view.onclicklistener介面 3 建立例項化介面物件 4 使用內部類 5 自己定義方法,配置android onclick屬性 import android.media.jetplayer.onjeteventlistener i...