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

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

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

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

我们具体看效果图:

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><FrameLayoutandroid:clickable="true"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/forcegroundstring"/></FrameLayout>
</LinearLayout>

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

无background ,无foreground

这时候我们给FrameLayout加上
android:background="@color/colorPrimary"。效果变成这样:

有background,无foreground

我们再给FrameLayout加上
android:foreground="@color/colorAccent"。效果变成这样:

有foreground,有background

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

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

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

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

    <FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:clickable="true"android:foreground="#5fC0C0C0"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/forcegroundstring" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/app_name" /></LinearLayout></FrameLayout>

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

因为属性能设置为drawable,我们自然就想到了也可以使用 selector drawable,在点击时套上drawable来实现类似点击效果的功能。
比如那种点击查看谜底的功能就可以简单用这种方法实现

未点击

已点击

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="世界上最帅的程序员是谁?点击下方查看谜底答案"/><FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:clickable="true"android:foreground="@drawable/forceground_drawable"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="世界上最帅的程序员是青蛙要fly,世界上最好用的语言是PHP" /></LinearLayout></FrameLayout></LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@color/colorAccent1"/><item android:drawable="@color/colorAccent2" />
</selector>
    <color name="colorAccent1">#00ffffff</color><color name="colorAccent2">#ffc0c0c0</color>

缺陷:

需要注意,前景的支持是在 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;

大家可以参考这篇:

http://blog.csdn.net/zhuoxiuwu/article/details/50976145
自定义实现TextView支持foreground功能。

作者:青蛙要fly
链接:https://www.jianshu.com/p/b5ecd39ed494
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

android:foreground与android:background相关推荐

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

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

  2. android:foreground

    foreground 前景色 foreground 也就是前景色,它与background相对应,顾名思义,它指定的drawable是在view视图的上方绘制的. 开发实例 1.实现遮罩层: < ...

  3. 邊實驗邊分析 - Android Foreground Service的使用

    邊實驗邊分析 - Android Foreground Service的使用 一.簡述 二.Android LowMemoryKiller 介紹 三.startService方法的調用行爲測試 給Se ...

  4. Android 关于android:foreground设置无效的问题

    最近遇到了水波纹问题,解决的方案有两种 1. android:background="@drawable/selecterDrawable" 2. android:foregrou ...

  5. android:windowBackground 和 android:background 的区别

    通过问别人,我知道了android:windowBackground 和 android:background的区别 android:windowBackground 一般用于activity启动的时 ...

  6. android于src和background差额

    ImageView中XML属性src和background的差别: background会依据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小.不会进行拉伸.src是图片内容(前 ...

  7. Android 系统性能优化(81)---Android后台优化系列-background optimization-初识低耗电模式

    Android后台优化系列-background optimization-初识低耗电模式 〇. 序 当我们手机屏幕电量的时候,我们或在游戏,或在看视频,或在上网,屏幕是一个很耗电的组件,在电量消耗方 ...

  8. android中src和background区别

    android中src和background区别 ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放 ...

  9. 安卓Android Studio Button按钮background不生效无效问题

    今天又是无语的一天...... 在学习安卓按钮这部分的时候,我新建了xml资源文件用来专门配置按钮的样式: <?xml version="1.0" encoding=&quo ...

最新文章

  1. MVC缓存OutPutCache学习笔记 (一) 参数配置
  2. fprintf函数的用法matlab_极力推荐这个Matlab教程
  3. android 75 新闻列表页面
  4. 数据库高级知识——主从复制
  5. CCActionEase想说爱你也不难(上)
  6. 生产力提升! 自己动手自定义Visual Studio 2019的 类创建模板,制作简易版Vsix安装包
  7. Activity与Intent机制的学习笔记--转自feisky
  8. SDN(软件定义网络)详解
  9. Matlab 除法取整
  10. 送书 | 图解机器学习—算法原理与Python语言实现
  11. 如何编制试算平衡表_在实际工作中,余额试算平衡通过编制试算平衡表进行。()...
  12. 互联网日报 | 6月20日 星期日 | 宁德时代否认强制员工购买特斯拉;小米618支付金额破190亿元;岚图FREE正式上市...
  13. 第16章 调色盘管理器
  14. WinXP_Vista禁止限制软件使用方法
  15. 2022年安全员-B证考试题库模拟考试平台操作
  16. 红米note4x开启root权限
  17. JUC学习 - 延迟队列 DelayQueue 详解
  18. Ueditor 百度编辑器 自定义工具栏与功能(ueditor+layui+ci)
  19. 如何监测耳机/麦克风设备插拔操作
  20. 【百度地图API】交你如何用百度地图搜索自己的数据!不需数据库!

热门文章

  1. 高德地图 JS API (地图/地图属性)
  2. 平衡二叉树(树的旋转)
  3. C#使用Microsoft.ACE.OLEDB来处理Excel数据
  4. 2021-01-12某证券公司后台开发岗位面试记录(二面)
  5. CLUSTER SLOTS
  6. linux基础56——uniq
  7. elementui 分页与tabe数据绑定
  8. 滁州市专精特新企业认定奖励及材料条件
  9. 什么是Javascript Hoisting?
  10. TPC-H:22个SQL语句说明