android:background大小,小Demo小知识-android:foreground与android:background

b5ecd39ed494

-----------------------------------------------前言君--------------------------------------------------

正好碰到了这个foreground属性平时没怎么用到过。这次用到,就特意的去看了下。在这里记录一下。

------------------------------------------------正文君--------------------------------------------

foreground 也就是前景色,它与background相对应,顾名思义,它指定的drawable是在view视图的上方绘制的。

我们具体看效果图:

比如当前我们的布局就是简单的:

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:clickable="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/forcegroundstring"

/>

布局中。我们再中包了一个这时候FrameLayout既没有设置background,也没设置foreground。我们可以看到效果是这样的:

b5ecd39ed494

无background ,无foreground

这时候我们给FrameLayout加上

android:background="@color/colorPrimary"。效果变成这样:

b5ecd39ed494

有background,无foreground

我们再给FrameLayout加上

android:foreground="@color/colorAccent"。效果变成这样:

b5ecd39ed494

有foreground,有background

发现当foreground有值的时候,连TextView的内容也看不到了。

-----------------------------------so 这样有个啥用?--------------------------------------

1.比如我们可以给他做个淡色的遮幕感:

b5ecd39ed494

这样不管FrameLayout里面有多少控件,我们不需要对控件一个个去设置,只要对FrameLayout的foreground做个颜色设置,如果设置为有透明度的灰色。

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:clickable="true"

android:foreground="#5fC0C0C0"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/forcegroundstring" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/app_name" />

2.简单实现一种点击查看的效果:

因为属性能设置为drawable,我们自然就想到了也可以使用 selector drawable,在点击时套上drawable来实现类似点击效果的功能。

比如那种点击查看谜底的功能就可以简单用这种方法实现:

b5ecd39ed494

未点击

b5ecd39ed494

已点击

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="世界上最帅的程序员是谁?点击下方查看谜底答案"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:clickable="true"

android:foreground="@drawable/forceground_drawable"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="世界上最帅的程序员是青蛙要fly,世界上最好用的语言是PHP" />

#00ffffff

#ffc0c0c0

缺陷:

需要注意,前景的支持是在 Android 6.0(也就是 API 23)才加入的;之前其实也有,不过只支持 FrameLayout,而直到 6.0 才把这个支持放进了 View 类里。

知道我为啥例子里面用的是FrameLayout来举例了吧。

Android在所有布局的基类 View 类中 就定义了 Foreground 这个属性,因为API 版本没有23的话,只有FrameLayout布局上设置该属性才会生效。观察View的代码发现这样一段。它只针对是FrameLayout的实例做获取该styleable的操作。

case R.styleable.View_foreground:

if (targetSdkVersion >= VERSION_CODES.M || this instanceof FrameLayout) {

setForeground(a.getDrawable(attr));

}

break;

case R.styleable.View_foregroundGravity:

if (targetSdkVersion >= VERSION_CODES.M || this instanceof FrameLayout) {

setForegroundGravity(a.getInt(attr, Gravity.NO_GRAVITY));

}

break;

大家可以参考这篇:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值