android:background大小,小Demo小知识-android:foreground与android:background
-----------------------------------------------前言君--------------------------------------------------
正好碰到了這個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。我們可以看到效果是這樣的:
無background ,無foreground
這時候我們給FrameLayout加上
android:background="@color/colorPrimary"。效果變成這樣:
有background,無foreground
我們再給FrameLayout加上
android:foreground="@color/colorAccent"。效果變成這樣:
有foreground,有background
發現當foreground有值的時候,連TextView的內容也看不到了。
-----------------------------------so 這樣有個啥用?--------------------------------------
1.比如我們可以給他做個淡色的遮幕感:
這樣不管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來實現類似點擊效果的功能。
比如那種點擊查看謎底的功能就可以簡單用這種方法實現:
未點擊
已點擊
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;
大家可以參考這篇:
總結
以上是生活随笔為你收集整理的android:background大小,小Demo小知识-android:foreground与android:background的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cursor 过滤 android,An
- 下一篇: cad画线中心快捷键(cad中心线快捷键