android Button ImageButton 區別

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.