日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。