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

来源:互联网 发布:怎样申请多个淘宝小号 编辑:程序博客网 时间:2024/05/22 17:21

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;        }    }}

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

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

阅读全文
0 0
原创粉丝点击