关于background与backgroundTint的区别

随笔

今天在学习时,设计背景色,发现提醒我不要用background属性

所以我在这说一下background与backgroundTint的区别:

//这是backgroundTint的源码
        <!-- Tint to apply to the background. -->
        <attr name="backgroundTint" format="color" />
//这是background的源码
		<!-- A drawable to use as the background.  This can be either a reference
             to a full drawable resource (such as a PNG image, 9-patch,
             XML state list description, etc), or a solid color such as "#ff000000"
            (black). -->
        <attr name="background" format="reference|color" />
  • 其实这个注释已经说了backgroundTint是应用到背景上的色彩
  • 而background是一个可绘制的背景,可以是一个完全可绘制资源的引用(例如图片、可调整大小位图9-patch、XML状态列表描述、etc),或者是纯色如黑色。

format格式的区别就是backgroundTint只能用颜色,而background有引用和颜色。

自然就是,如果你只是要给背景上纯色的话,建议用backgroundTint,如果你要用背景图片的话,就用background,可以分别提高对应的执行效率!!!

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <com.example.myapplication.GameView android:id="@+id/gameView" android:layout_width="match_parent" android:layout_height="match_parent"/> <TextView android:id="@+id/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:paddingTop="10dp" android:text="0" android:textColor="@color/black" android:textSize="18dp" /> <RelativeLayout android:visibility="gone" android:id="@+id/relative" android:layout_width="300dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@drawable/introdution" > <TextView android:id="@+id/scoreText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:backgroundTint="@color/black" android:text="分数:" android:textSize="18dp" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:backgroundTint="#0068B5" android:text="重新开始" android:textSize="18dp" /> </RelativeLayout> <RelativeLayout android:id="@+id/introdution" android:layout_width="300dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@drawable/shuoming"> <Button android:id="@+id/btn_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:backgroundTint="#0068B6" android:text="开始游戏" android:textSize="18dp" /> </RelativeLayout> </RelativeLayout>解释这段布局文件代码
06-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值