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背景图片。