BUTTON類結構圖:
由類結構圖,可以看出Button是繼承於TextView的,所以TextView的一些屬性也適用於Button控制項。
ImageButton類結構圖:
ImageButton就是用一個表徵圖代表了一些文字,它沒Android:text屬性。它由Android:src指定表徵圖的位置
android:src="@drawable/back_48"
其他屬性都和Button差不多
1、 如何設定按鈕的樣式?
通過Android:background設定
[java]
<Button android:id="@+id/myBtn1" android:text="按鈕1 設定背景樣式"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#fff000" />
2、如何設定背景表徵圖:
[java]
<Button android:id="@+id/myBtn6" android:text="按鈕6 設定背景表徵圖"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:background="@drawable/back_48"
/>
3、 如何設定按鈕的文字顏色
通過Android:textColor
[java]
<Button android:id="@+id/myBtn2" android:text="按鈕2 字型顏色"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#ff0000" />
nbsp;
4、 如何設定按鈕的文字樣式
通過android:textStyle
[java]
<Button android:id="@+id/myBtn3" android:text="按鈕3字型加粗"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#ff0000" android:textStyle="bold" />
5、 如何為按鈕添加監聽器註冊事件
方式1:通過setOnClickListener方式
[java]
myBtn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myBtn4.setText("setOnclickListener事件監聽註冊成功");
}
});
實踐演練:
1、 怎麼樣設定ImageButton的表徵圖位置
[html]
<ImageButton android:id="@+id/imgBtn01" android:src="@drawable/forward_48"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
2、 怎麼樣為ImageButton添加監聽器註冊事件
與Button一樣它照樣有兩種方式設定
方式1:通過onClickListener
[java]
imgBtn01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myTextView.setText("ImageButton的監聽事件");
}
});
方式2:通過XML檔案設定
[html]
<ImageButton android:id="@+id/imgBtn02" android:src="@drawable/back_48"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="ImageButtonXml" />
1、 設定透明度
[java]
imgBtn01.setAlpha(50);//設定透明度
作者:hjm4702192