導航:首頁 > 操作系統 > androidbutton添加背景

androidbutton添加背景

發布時間:2023-02-22 00:59:32

android中button上設置圖片

android中button上設置圖片的方法為:
1、自定義MyButton類
public class MyButton extends Button {
//This constructormust be
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context) {
super(context);
}
private Paint mPaint = null;
private String mText;
private int mX, mY;
public void onSetText(String text, int nLeft, int nBottom, int nTextSize,
int nTextColor) {
mPaint = new Paint();
mPaint.setTextSize(nTextSize);
mPaint.setColor(nTextColor);
this.mText = text;
this.mX = nLeft;
this.mY = nBottom;
}
private int mDownBmpId, mUpBmpId;
public void onSetBmp(int nDownID, int nUpID) {
this.mDownBmpId = nDownID;
this.mUpBmpId = nUpID;
}
@Override
public void onDraw(Canvas canvas) {
if (mPaint != null)
canvas.drawText(mText, mX, mY, mPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
super.setBackgroundResource(mDownBmpId);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
super.setBackgroundResource(mUpBmpId);
}
return super.onTouchEvent(event);
}
}

2、 在xml布局文件中添加MyButton控制項,像應用普通的Button控制項一樣。
<com.MyButton
android:id="@+id/test_btn" android:layout_width="120px"
android:layout_height="fill_parent" android:text="Test"
android:background="@drawable/btn_u" />
其中com.MyButton是你定義的MyButton類所在的包名
3、在onCreate()中載入MyButton控制項。
MyButton btn = (MyButton)findViewById(R.id.test_btn);
btn.onSetBmp(R.drawable.btn_d, R.drawable.btn_u);
其中btn_d表示為按下btn時背景圖片,btn_u為默認狀態下btn背景圖片。

⑵ android button 點擊後如何更換背景,然後點擊其他按鈕時該按鈕背景換回來

1、要更換背景的按鈕,id是myself。其他按鈕(以一個按鈕未代表)

java">ButtonmyButton=(Button)findViewById(R.id.myself);//要改變背景的按鈕
ButtonotherButton=(Button)findViewById(R.id.xxxx);//其他按鈕

2、定義Listener,如果id是myself,則改變為其他背景,否則變回來

OnClickListenercl=newOnClickListener(){
@Override
publicvoidonClick(Viewv){
if(v.getId()==R.id.myself){//如果是myself按鈕,則設置一種背景
myButton.setBackgroundResource(R.drawable.xxxx1);
}else{//如果不是myself按鈕,則設置回來。
myButton.setBackgroundResource(R.drawable.xxxx2);
}
}
}

3、按鈕設置監聽

myButton.setOnClickListener(cl);
otherButton.setOnClickListener(cl);

⑶ android Button 怎麼把背景設置透明

Android控制項設置邊框,或者背景可以使用XML來配置,背景透明只需要設置solid 的值為 #00000000即可,前面兩位是透明度,後面6位是RGB顏色值,具體示例代碼如下:
1.在drawable新建一個 buttonstyle.xml的文件,內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 連框顏色值 --><item>
<shape>
<solid android:color="#ff0000" />
</shape>
</item>
<!-- 主體背景顏色值 -->
<item android:bottom="3dp" android:right="3dp">
<shape>
<solid android:color="#ffffff" />
<padding android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
</layer-list>

2.然後在布局文件裡面引入這個xml,示例代碼如下:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:background="@drawable/buttonstyle" />

⑷ android中設置Button點擊切換按鈕的背景圖片,點第一次換了一張背景,怎麼設置再次換張背景

在onclick事件下 設置按鈕的背景圖片,但是要隨機,把你想要顯示的圖片放到數組裡面,然後隨機下角標,選出來的圖片作為背景圖片就可以了,每次點擊就會生成一個圖片,想要不重復,那就寫個去重復的方法就行了。沒理解的話追問把

閱讀全文

與androidbutton添加背景相關的資料

熱點內容
新手學電腦編程語言 瀏覽:891
雲空間在哪個文件夾 瀏覽:926
編程游戲小貓抓小魚 瀏覽:790
安卓dosbox怎麼打開 瀏覽:774
伺服器無影響是怎麼回事 瀏覽:952
比德電子采購平台加密 瀏覽:202
加密貨幣400億 瀏覽:524
植發2次加密 瀏覽:44
vc6查看編譯的錯誤 瀏覽:595
心理大全pdf 瀏覽:1002
區域鏈加密幣怎麼樣 瀏覽:343
查找命令符 瀏覽:95
壓縮工具zar 瀏覽:735
白盤怎麼解壓 瀏覽:475
辰語程序員學習筆記 瀏覽:47
程序員被公司勸退 瀏覽:523
java三子棋 瀏覽:693
加密空間怎麼強制進入 瀏覽:345
ug分割曲線命令 瀏覽:209
學碼思程序員 瀏覽:610