天天看點

android+3.1.2+imagebutton監聽,android Button ImageButton 差别

android Button ImageButton 差別

BUTTON類結構圖:

android+3.1.2+imagebutton監聽,android Button ImageButton 差别

由類結構圖,可以看出Button是繼承于TextView的,是以TextView的一些屬性也适用于Button控件。

ImageButton類結構圖:

android+3.1.2+imagebutton監聽,android Button ImageButton 差别

ImageButton就是用一個圖示代表了一些文字,它沒Android:text屬性。它由Android:src指定圖示的位置

android:src="@drawable/back_48"

其他屬性都和Button差不多

1、  如何設定按鈕的樣式?

通過Android:background設定

android:layout_width="fill_parent" android:layout_height="wrap_content"

android:background="#fff000" />

2、如何設定背景圖示:

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:textStyle="bold" android:background="@drawable/back_48"

/>

3、  如何設定按鈕的文字顔色

通過Android:textColor

android:layout_width="fill_parent" android:layout_height="wrap_content"

android:textColor="#ff0000" />

nbsp;

4、  如何設定按鈕的文字樣式

通過android:textStyle

android:layout_width="fill_parent" android:layout_height="wrap_content"

android:textColor="#ff0000" android:textStyle="bold" />

5、  如何為按鈕添加監聽器注冊事件

方式1:通過setOnClickListener方式

myBtn4.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

myBtn4.setText("setOnclickListener事件監聽注冊成功");

}

});

實踐演練:

1、  怎麼樣設定ImageButton的圖示位置

android:layout_width="wrap_content" android:layout_height="wrap_content" />

2、  怎麼樣為ImageButton添加監聽器注冊事件

與Button一樣它照樣有兩種方式設定

方式1:通過onClickListener

imgBtn01.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

myTextView.setText("ImageButton的監聽事件");

}

});

方式2:通過XML檔案設定

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:onClick="ImageButtonXml" />

1、  設定透明度

imgBtn01.setAlpha(50);//設定透明度