setBackground(),setBackgroundResource(),setBackgroundColor(),setBackgroundDrawable()的区别和用法

setBackground(),setBackgroundResource(),setBackgroundColor()和setBackgroundDrawable()这几个方法都可以对控件的颜色进行设置,setBackground(),setBackgroundResource()和setBackgroundDrawable()可以对背景的样式进行设置,但他们之间又有一定的区别

  • SetBackground(Drawable background)其参数为一个Drawable对象,目的是设置view的背景图片,Drawable对象可以这样获取getResources().getDrawable(R.drawable.xx),还可以是Context.getResource().getColor(R.color.white)

  • setBackgroundColor(int color)其参数为一个颜色值,其目的是设置一个view的背景颜色

  • setBackgroundDrawable(Drawable background)和SetBackground有异曲同工之妙,都是通过传入一个Drawable对象设置view控件的背景图片

  • setBackgroundResource(int resid)它也是设置一个view的背景图片,只不过传入的是一个drawable的id值或者color颜色值

setBackground和setBackgroundDrawable方法的区别:两者都是传入一个Drawable对象,但setBackground实在API16以上才提供的方法,在API16及以下则是只能使用setBackgroundDrawable的方法,如果在API16 以上使用setBackgroundDrawable方法则会提示该方法过时(其实它还能用,只不过android官方不建议你用,给这个方法画了个横线,可以在兼容PAI16 以下系统的APP里使用这个方法。),对于这种情况,可以通过对系统判断来决定使用什么方法,给个小代码:

//在API16以前使用setBackgroundDrawable,在API16以后使用setBackground 
// API16<---->Android 4.1
 private void setBackgroundOfVersion(View view, Drawable drawable) {  
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  
            //Android系统大于等于API16,使用setBackground  
            view.setBackground(drawable);  
        } else {  
            //Android系统小于API16,使用setBackgroundDrawable  
            view.setBackgroundDrawable(drawable);  
        }  
    }  

setBackgroundResource和setBackgroundColor的区别:一开始我也不是很明白,看了一些大神的博客后发现还是有区别的,setBackgroundResource设置的是最底层的颜色,当改变完颜色以后,如果布局在xml文件中默认颜色是white,会被white遮盖掉。setBackgroundColor设置的是中间层的颜色,相当于XML文件里的颜色setBackgroundColor(context.getResouce().getColor(R.color.XXX))可以理解为改变的是最上层的颜色,不管xml布局中的颜色是什么色,使用了setBackgroundColor,就会在布局颜色上层刷上颜色,所以就会显色。
这只是我的个人理解,如果哪里有问题,希望在评论区指出来,万分感谢!

关于setBackground和setBackgroundDrawable传入的对象的书写问题:如果是以下格式不会有问题:btn.setBackground(getResources().getDrawable(R.drawable.buttonstyle));
btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttonstyle));

若是这种书写格式则会有问题:btn.setBackground(getDrawable(R.drawable.buttonstyle));
btn.setBackgroundDrawable(getDrawable(R.drawable.buttonstyle));
,经过测试发现,在android5.0及以上的系统不会有问题,和上面的格式一样,但是在android4.4及以下的系统则会出现闪退现象,所以这种书写格式在android4.4及以下的系统不适用,建议在写代码的时候使用第一种书写格式,避免不必要的的问题!

关于setBackgroundColor:如果在XML文件里使用background属性设置了样式,改变原有样式,如果在java代码里使用setBackgroundColor的方法则会把原来XML里的样式给覆盖掉,变成默认样式。如果涉及到样式的修改,一般不用setBackgroundColor方法。

给个自己写的小Demo代码:

XML文件activity_button.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.a14392.aboutbackground.ButtonBackground">

    <TextView
        android:layout_marginTop="36dp"
        android:textSize="18sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关于设置background的问题"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_1"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:background="@drawable/buttonstyle"
        android:text="setBackground属性"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:text="setBackgroundResource属性"
        android:textSize="18sp"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:background="@drawable/buttonstyle"/>
    <Button
        android:id="@+id/btn_3"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:text="setBackgroundColor属性"
        android:textSize="18sp"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:background="@drawable/buttonstyle"/>
    <Button
        android:id="@+id/btn_4"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:text="setBackgroundDrawable属性"
        android:textSize="18sp"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:background="@drawable/buttonstyle"/>
</LinearLayout>

drawable文件夹里的两个样式文件buttonstyle.xml和buttonstyle2.xml

buttonstyle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="6dp"/>
            <solid android:color="#07c3f7"/>
        </shape>
    </item>
</selector>

buttonstyle2.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="6dp"/>
            <solid android:color="#0d8ced"/>
        </shape>
    </item>
</selector>

ButtonBackground.java

public class ButtonBackground extends AppCompatActivity implements View.OnClickListener {

    public Button btn1,btn2,btn3,btn4;
    int b1=0,b2=0,b3=0,b4=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        findview();
    }

    void findview(){
        btn1=(Button) findViewById(R.id.btn_1);
        btn2=(Button) findViewById(R.id.btn_2);
        btn3=(Button) findViewById(R.id.btn_3);
        btn4=(Button) findViewById(R.id.btn_4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_1:
                if(b1==0){
                    btn1.setBackground(getResources().getDrawable(R.drawable.buttonstyle2));
                    /*btn1.setBackground(getDrawable(R.drawable.buttonstyle2));//这种格式在android4.4及以下的系统会出现闪退问题*/
                    b1=1;
                }else {
                    btn1.setBackground(getResources().getDrawable(R.drawable.buttonstyle));
                    /*btn1.setBackground(getDrawable(R.drawable.buttonstyle));//这种格式在android4.4及以下的系统会出现闪退问题*/
                    b1=0;
                }
                break;
            case R.id.btn_2:
                if(b2==0){
                    btn2.setBackgroundResource(R.drawable.buttonstyle2);
                    b2=1;
                }else {
                    btn2.setBackgroundResource(R.drawable.buttonstyle);
                    b2=0;
                }
                break;
            case R.id.btn_3:
                if(b3==0){
                    btn3.setBackgroundColor(Color.parseColor("#0d8ced"));
                    b3=1;
                }else {
                    btn3.setBackgroundColor(Color.parseColor("#07c3f7"));
                    b3=0;
                }
                break;
            case R.id.btn_4:
                if(b4==0){
                    btn4.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttonstyle));
                   /* btn4.setBackgroundDrawable(getDrawable(R.drawable.buttonstyle));这种格式在android4.4及以下的系统会出现闪退问题*/
                    b4=1;
                }else {
                    btn4.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttonstyle2));
                   /* btn4.setBackgroundDrawable(getDrawable(R.drawable.buttonstyle));这种格式在android4.4及以下的系统会出现闪退问题*/
                    b4=0;
                }
                break;
            default:break;
        }
    }
}

看一下效果图:
点击前的效果
点击后的效果

如有错误与不足,欢迎评论指出,谢谢!!!

  • 19
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
setbackground用于设置View或ViewGroup的背景。该方法有多个重载形式,可以接受各种类型的参数。 常用的几种setbackground方法的用法如下: 1. setBackground(Drawable background) 该方法接受一个Drawable对象作为参数,可以设置View或ViewGroup的背景为任意类型的Drawable。例如: ```java // 设置View的背景为红色 view.setBackground(new ColorDrawable(Color.RED)); // 设置View的背景为图片 view.setBackground(getResources().getDrawable(R.drawable.background_image)); ``` 2. setBackgroundResource(int resId) 该方法接受一个资源ID作为参数,可以设置View或ViewGroup的背景为指定资源ID对应的Drawable。例如: ```java // 设置View的背景为红色 view.setBackgroundResource(R.color.red); // 设置View的背景为图片 view.setBackgroundResource(R.drawable.background_image); ``` 3. setBackgroundColor(int color) 该方法接受一个颜色值作为参数,可以设置View或ViewGroup的背景为单色。例如: ```java // 设置View的背景为红色 view.setBackgroundColor(Color.RED); ``` 4. setBackgroundTintList(ColorStateList tint) 该方法接受一个ColorStateList对象作为参数,可以设置View或ViewGroup的背景色调(tint)。例如: ```java // 设置View的背景色调为蓝色 view.setBackgroundTintList(ColorStateList.valueOf(Color.BLUE)); ``` 使用setbackground方法可以方便地设置View或ViewGroup的背景,使应用程序更加美观和易于使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值