android:background大小,小Demo小知识-android:foreground与android:background
-----------------------------------------------前言君--------------------------------------------------
正好碰到了這個(gè)foreground屬性平時(shí)沒(méi)怎么用到過(guò)。這次用到,就特意的去看了下。在這里記錄一下。
------------------------------------------------正文君--------------------------------------------
foreground 也就是前景色,它與background相對(duì)應(yīng),顧名思義,它指定的drawable是在view視圖的上方繪制的。
我們具體看效果圖:
比如當(dāng)前我們的布局就是簡(jiǎn)單的:
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"
/>
布局中。我們?cè)僦邪艘粋€(gè)這時(shí)候FrameLayout既沒(méi)有設(shè)置background,也沒(méi)設(shè)置foreground。我們可以看到效果是這樣的:
無(wú)background ,無(wú)foreground
這時(shí)候我們給FrameLayout加上
android:background="@color/colorPrimary"。效果變成這樣:
有background,無(wú)foreground
我們?cè)俳oFrameLayout加上
android:foreground="@color/colorAccent"。效果變成這樣:
有foreground,有background
發(fā)現(xiàn)當(dāng)foreground有值的時(shí)候,連TextView的內(nèi)容也看不到了。
-----------------------------------so 這樣有個(gè)啥用?--------------------------------------
1.比如我們可以給他做個(gè)淡色的遮幕感:
這樣不管FrameLayout里面有多少控件,我們不需要對(duì)控件一個(gè)個(gè)去設(shè)置,只要對(duì)FrameLayout的foreground做個(gè)顏色設(shè)置,如果設(shè)置為有透明度的灰色。
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.簡(jiǎn)單實(shí)現(xiàn)一種點(diǎn)擊查看的效果:
因?yàn)閷傩阅茉O(shè)置為drawable,我們自然就想到了也可以使用 selector drawable,在點(diǎn)擊時(shí)套上drawable來(lái)實(shí)現(xiàn)類(lèi)似點(diǎn)擊效果的功能。
比如那種點(diǎn)擊查看謎底的功能就可以簡(jiǎn)單用這種方法實(shí)現(xiàn):
未點(diǎn)擊
已點(diǎn)擊
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="世界上最帥的程序員是誰(shuí)?點(diǎn)擊下方查看謎底答案"
/>
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,世界上最好用的語(yǔ)言是PHP" />
#00ffffff
#ffc0c0c0
缺陷:
需要注意,前景的支持是在 Android 6.0(也就是 API 23)才加入的;之前其實(shí)也有,不過(guò)只支持 FrameLayout,而直到 6.0 才把這個(gè)支持放進(jìn)了 View 類(lèi)里。
知道我為啥例子里面用的是FrameLayout來(lái)舉例了吧。
Android在所有布局的基類(lèi) View 類(lèi)中 就定義了 Foreground 這個(gè)屬性,因?yàn)锳PI 版本沒(méi)有23的話(huà),只有FrameLayout布局上設(shè)置該屬性才會(huì)生效。觀(guān)察View的代碼發(fā)現(xiàn)這樣一段。它只針對(duì)是FrameLayout的實(shí)例做獲取該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;
大家可以參考這篇:
總結(jié)
以上是生活随笔為你收集整理的android:background大小,小Demo小知识-android:foreground与android:background的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cursor 过滤 android,An
- 下一篇: android 处理http状态码,Ok