
-----------------------------------------------前言君--------------------------------------------------
正好碰到了這個foreground屬性平時沒怎么用到過。這次用到,就特意的去看了下。在這里記錄一下。
------------------------------------------------正文君--------------------------------------------
foreground 也就是前景色,它與background相對應(yīng),顧名思義,它指定的drawable是在view視圖的上方繪制的。
我們具體看效果圖:
比如當(dāng)前我們的布局就是簡單的:
<?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">
<FrameLayout
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/forcegroundstring"
/>
</FrameLayout>
</LinearLayout>
布局中。我們再<FrameLayout>中包了一個<TextView>這時候FrameLayout既沒有設(shè)置background,也沒設(shè)置foreground。我們可以看到效果是這樣的:

這時候我們給FrameLayout加上
android:background="@color/colorPrimary"。效果變成這樣:

我們再給FrameLayout加上
android:foreground="@color/colorAccent"。效果變成這樣:

發(fā)現(xiàn)當(dāng)foreground有值的時候,連TextView的內(nèi)容也看不到了。
-----------------------------------so 這樣有個啥用?--------------------------------------
1.比如我們可以給他做個淡色的遮幕感:

這樣不管FrameLayout里面有多少控件,我們不需要對控件一個個去設(shè)置,只要對FrameLayout的foreground做個顏色設(shè)置,如果設(shè)置為有透明度的灰色。
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="#5fC0C0C0"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/forcegroundstring" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</LinearLayout>
</FrameLayout>
2.簡單實現(xiàn)一種點擊查看的效果:
因為屬性能設(shè)置為drawable,我們自然就想到了也可以使用 selector drawable,在點擊時套上drawable來實現(xiàn)類似點擊效果的功能。
比如那種點擊查看謎底的功能就可以簡單用這種方法實現(xiàn):


<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="世界上最帥的程序員是誰?點擊下方查看謎底答案"
/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="@drawable/forceground_drawable"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android: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布局上設(shè)置該屬性才會生效。觀察View的代碼發(fā)現(xiàn)這樣一段。它只針對是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
自定義實現(xiàn)TextView支持foreground功能。