導航:首頁 > 操作系統 > 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添加背景相關的資料

熱點內容
什麼java編譯器支持中文 瀏覽:561
香港伺服器如何做代理 瀏覽:199
pdf寫入 瀏覽:984
高爾夫電台怎麼添加到文件夾 瀏覽:237
四川麻將一般下哪個app 瀏覽:864
反編譯exe腳本 瀏覽:460
源碼文件夾怎麼編譯到固件中 瀏覽:912
ERp列印伺服器錯誤怎麼弄 瀏覽:113
蚌埠u盤加密軟體有哪些 瀏覽:180
前端如何認證伺服器 瀏覽:554
linux切換db2用戶命令 瀏覽:308
相片如何用電解壓 瀏覽:908
碩士程序員去學校當老師 瀏覽:122
pythonstr提取到字典 瀏覽:820
程序員那麼可愛有人看上陸漓了 瀏覽:878
php正則提取圖片 瀏覽:105
pythonlinuxdjango 瀏覽:564
php中文返回亂碼 瀏覽:91
宿舍裝的電信怎麼加密 瀏覽:747
為什麼壓縮文件解壓後變少了 瀏覽:428