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

熱點內容
如何在伺服器管理器添加角色 瀏覽:395
程序員那麼可愛陸漓出車禍失憶了 瀏覽:138
51單片機ad轉換教程 瀏覽:436
TPM怎麼加密文件夾 瀏覽:81
找工作用什麼app靠譜 瀏覽:915
從程序員轉產品經理 瀏覽:793
linux查詢序列號 瀏覽:251
vi輸入命令 瀏覽:654
程序員節幾月份合適 瀏覽:291
凱迪app是什麼東西 瀏覽:765
linuxjavapath環境變數 瀏覽:971
pdf的更多命令在哪裡 瀏覽:168
c語言經過編譯的後綴 瀏覽:875
怎麼做一款app 瀏覽:969
小紅書看過的視頻在哪個文件夾 瀏覽:536
中興雲伺服器市場份額 瀏覽:407
內網系統報文加密 瀏覽:210
centeroslinux 瀏覽:910
xcode如何編譯大型項目 瀏覽:59
平安京編譯器 瀏覽:869