androidbutton背景
A. 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" />
B. 【Android】關於Button背景色/樣式設置失效
在設置button背景顏色後,button背景仍然為藍色。如下所示:
在使用Android Studio 4.1+ 進行開發時,創建的項目默認的主題是 Theme.MaterialComponents.DayNight.DarkActionBar
。所有Button都是Material類型的Button,默認使用主題色。
解決方法:
android - How to change the color of a button? - Stack Overflow
C. android 按鈕設置點擊更換imagebutton背景
button.setBackgroundResource(R.drawable.beijing1);
上面是改變按鈕背景的代碼
可以做兩組圖片,分別為button1的選中和為選擇狀態、button2的選中和為選擇狀態,讓後再button1和button2的點擊事件中,對兩個按鈕的背景進行改變,就可以了
D. Android studio的問題,為什麼button無論設置什麼背景,都是藍色的,沒有一點變化
這個就看mipmap目錄下的btn文件是啥顏色的了,如果沒問題的話,再看看是不是有其他布局覆蓋在Button上了。ps:你新建一個button 不設置任何背景屬性,去排查一下是哪方面的問題
E. Android中的Button怎麼在點擊更換背景點擊後又恢復原來的背景
只需要給Button配置一個Selector背景選擇器即可實現。
1.創建mylist_view.xml文件
首先在res目錄下新建drawable文件夾,再在新建的drawable文件夾中新建mylist_view.xml,其目錄結構為:res/drawable/mylist_view.xml。
2.根據具體需求編輯mylist_view.xml文件
新建mylist_view.xml文件後,在沒有添加任何屬性時其內部代碼結構為:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>
3.下面就可以根據項目需求,在其內部定義為自己想要的樣式了,主要屬性如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默認時的背景圖片-->
<item android:drawable="@drawable/pic1" />
<!-- 沒有焦點時的背景圖片 -->
<item android:state_window_focused="false" android:drawable="@drawable/pic1" />
<!-- 非觸摸模式下獲得焦點並單擊時的背景圖片 -->
<item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic2" />
<!-- 觸摸模式下單擊時的背景圖片-->
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic3" />
<!--選中時的圖片背景-->
<item android:state_selected="true" android:drawable="@drawable/pic4" />
<!--獲得焦點時的圖片背景-->
<item android:state_focused="true" android:drawable="@drawable/pic5" />
</selector>
4.引用mylist_view.xml文件
android:background="@drawable/mylist_view"
F. 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);
G. 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背景圖片。