ImageButtonhtml
類結構圖:java
![](http://static.javashuo.com/static/loading.gif)
ImageButton就是用一個圖標表明瞭一些文字,它沒Android:text屬性。它由Android:src指定圖標的位置linux
android:src="@drawable/back_48"android
其餘屬性都和Button差很少ide
實踐演練:字體
一、 怎麼樣設置ImageButton的圖標位置spa
- <ImageButton android:id="@+id/imgBtn01" android:src="@drawable/forward_48"
-
- android:layout_width="wrap_content" android:layout_height="wrap_content" />
二、 怎麼樣爲ImageButton添加監聽器註冊事件xml
與Button同樣它照樣有兩種方式設置htm
方式1:經過onClickListener繼承
- imgBtn01.setOnClickListener(new OnClickListener() {
-
-
-
- @Override
-
- public void onClick(View v) {
-
-
-
- myTextView.setText("ImageButton的監聽事件");
-
- }
-
- });
方式2:經過XML文件設置
- <ImageButton android:id="@+id/imgBtn02" android:src="@drawable/back_48"
-
- android:layout_width="wrap_content" android:layout_height="wrap_content"
-
- android:onClick="ImageButtonXml" />
最終效果:
![](http://static.javashuo.com/static/loading.gif)
一、 設置透明度
imgBtn01.setAlpha(50);//設置透明度
![](http://static.javashuo.com/static/loading.gif)
Button
類結構圖:
![](http://static.javashuo.com/static/loading.gif)
由類結構圖,能夠看出Button是繼承於TextView的,因此TextView的一些屬性也適用於Button控件。對於Button控件,相信大多數開發人員是再熟悉不過了,那麼廢話很少說,直接應用實踐吧
實踐演練:
一、 如何設置按鈕的樣式?
經過Android:background設置
- <Button android:id="@+id/myBtn1" android:text="按鈕1 設置背景樣式"
-
- android:layout_width="fill_parent" android:layout_height="wrap_content"
-
- android:background="#fff000" />
二、如何設置背景圖標:
- <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"
-
- />
三、 如何設置按鈕的文字顏色
經過Android:textColor
- <Button android:id="@+id/myBtn2" android:text="按鈕2 字體顏色"
-
- android:layout_width="fill_parent" android:layout_height="wrap_content"
-
- android:textColor="#ff0000" />
-
- nbsp;
四、 如何設置按鈕的文字樣式
經過android:textStyle
- <Button android:id="@+id/myBtn3" android:text="按鈕3字體加粗"
-
- android:layout_width="fill_parent" android:layout_height="wrap_content"
-
- android:textColor="#ff0000" android:textStyle="bold" />
五、 如何爲按鈕添加監聽器註冊事件
方式1:經過setOnClickListener方式
- myBtn4.setOnClickListener(new OnClickListener() {
-
-
-
- @Override
-
- public void onClick(View v) {
-
- myBtn4.setText("setOnclickListener事件監聽註冊成功");
-
-
-
- }
-
- });
方式2:經過XML文件的Android:onClick指定方法
- <Button android:id="@+id/myBtn4" android:text="按鈕4 經過setOnclickListener註冊監聽事件"
-
- android:layout_width="fill_parent" android:layout_height="wrap_content"
-
- android:textStyle="bold" android:onClick="selfDestruct" />
指定了sefDestrut
因此在Activity寫上一個這樣的方法就能夠了
- public void selfDestruct(View v) {
-
-
-
- myBtn5.setText("XML方式事件監聽註冊成功");
-
- System.out.println("------view v--------");
-
-
-
- }
上面的綜合例子效果圖爲:
![](http://static.javashuo.com/static/loading.gif)