Android開發(fā)輕松實(shí)現(xiàn)帶文字的圖片按鈕.doc

Android開發(fā)輕松實(shí)現(xiàn)帶文字的圖片按鈕.doc

ID:62039704

大?。?27.00 KB

頁(yè)數(shù):5頁(yè)

時(shí)間:2021-04-16

Android開發(fā)輕松實(shí)現(xiàn)帶文字的圖片按鈕.doc_第1頁(yè)
Android開發(fā)輕松實(shí)現(xiàn)帶文字的圖片按鈕.doc_第2頁(yè)
Android開發(fā)輕松實(shí)現(xiàn)帶文字的圖片按鈕.doc_第3頁(yè)
Android開發(fā)輕松實(shí)現(xiàn)帶文字的圖片按鈕.doc_第4頁(yè)
Android開發(fā)輕松實(shí)現(xiàn)帶文字的圖片按鈕.doc_第5頁(yè)
資源描述:

《Android開發(fā)輕松實(shí)現(xiàn)帶文字的圖片按鈕.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。

1、個(gè)人收集整理勿做商業(yè)用途Android開發(fā)輕松實(shí)現(xiàn)帶文字的ImageButton實(shí)際上,ImageButton是不能添加文字的。要實(shí)現(xiàn)帶文字的ImageButton的方法很多,這里僅列舉一種方法:自定義一個(gè)繼承自ImageButton的類,然后Override它的onDraw(Canvascanvas)方法。具體步驟如下:1)新建一個(gè)Android工程,例如工程名:TestImageButton。怎么建工程?不用我多說(shuō)了吧。2)新建一個(gè)MyImageButton類,繼承android.widget.ImageButton3)為類MyImageButton添加成員

2、函數(shù),詳細(xì)代碼如下:package?。鬳st.swjtu.edu;importandroid.content.Context;import android.graphics.Canvas;個(gè)人收集整理勿做商業(yè)用途import android.graphics.Paint;importandroid.util.AttributeSet;import android.widget.ImageButton;publicclassMyImageButtonextendsImageButton{publicStringtext= null;//要顯示的文字?public 

3、float(yī)textX,textY;? //文本顯示的坐標(biāo)位置?publicintcolor; ?。淖值念伾 ublicMyImageButton(Context context, AttributeSet attrs){  super(context,attrs);textX=20;  textY=60; }  //設(shè)置需要顯示的文本publicvoidsetText(Stringtext){?。鬶is.text =text;  //設(shè)置文字   }  //設(shè)置文本顯示的顏色 publicvoid setColor(intcolor){  this.co

4、lor =color; //設(shè)置文字顏色?。   ?/設(shè)置顯示文本的X、Y坐標(biāo) publicvoidsetPosition(float(yī)XX,float YY){?textX=?。豖;?textY=YY; }@Override?protectedvoidonDraw(Canvascanvas){?//TODOAuto-generated method stub?super.onDraw(canvas);?Paint paint=newPaint();  paint.setTextAlign(Paint.Align.CENTER);      paint.setC

5、olor(color);    canvas.drawText(text,textX,textY,paint);//繪制文字}  }個(gè)人收集整理勿做商業(yè)用途4)在布局文件中引用:

6、on="vertical"   android:background="#9CFFC1"><TextView   android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/hello"/> ?。紅est.swjtu.edu.MyImageButton  android:id="@+id/button01" android:layout_width="wrap_content"?android:layout_height="wrap

7、_content"  android:layout_marginLeft="25dp"?android:background="@drawable/video1" ?。?5)在啟動(dòng)Activity的onCreate方法中添加文字packagetest.swjtu.edu;importtest.swjtu.edu.MyImageButton;importandroid.a(chǎn)pp.Activity;import android.graphics.Color;import android.os.Bundle;publicclass Test

8、ImageButtonA

當(dāng)前文檔最多預(yù)覽五頁(yè),下載文檔查看全文

此文檔下載收益歸作者所有

當(dāng)前文檔最多預(yù)覽五頁(yè),下載文檔查看全文
溫馨提示:
1. 部分包含數(shù)學(xué)公式或PPT動(dòng)畫的文件,查看預(yù)覽時(shí)可能會(huì)顯示錯(cuò)亂或異常,文件下載后無(wú)此問(wèn)題,請(qǐng)放心下載。
2. 本文檔由用戶上傳,版權(quán)歸屬用戶,天天文庫(kù)負(fù)責(zé)整理代發(fā)布。如果您對(duì)本文檔版權(quán)有爭(zhēng)議請(qǐng)及時(shí)聯(lián)系客服。
3. 下載前請(qǐng)仔細(xì)閱讀文檔內(nèi)容,確認(rèn)文檔內(nèi)容符合您的需求后進(jìn)行下載,若出現(xiàn)內(nèi)容與標(biāo)題不符可向本站投訴處理。
4. 下載文檔時(shí)可能由于網(wǎng)絡(luò)波動(dòng)等原因無(wú)法下載或下載錯(cuò)誤,付費(fèi)完成后未能成功下載的用戶請(qǐng)聯(lián)系客服處理。