Android 背景透明度設(shè)置總結(jié)
一、寫在前面的
在需求上遇到背景設(shè)置透明度還是比較常見的,設(shè)置透明度有幾種方式,但是不同的場景應(yīng)用下,不同的方式可能會(huì)出現(xiàn)一些問題。針對開發(fā)過程中的需求做以下總結(jié)。
二、先看效果圖
圖1、
圖2、
圖3、
圖4
介紹:圖1、藍(lán)色頭部和輸入框背景初始狀態(tài)
圖2、點(diǎn)擊按鈕01,輸入框的透明度不起作用,和title的透明度一樣
圖3、點(diǎn)擊按鈕02,背景透明度設(shè)置正常,但是可能會(huì)對全局的背景有影響
圖4、點(diǎn)擊按鈕03,背景透明度設(shè)置正常,具體原因代碼注釋有提到
三、再加上代碼
按鈕點(diǎn)擊
public void button01(View view){ // search透明度不起作用 title.setAlpha(0.2f); search.setAlpha(0.8f); } public void button02(View view){ // 在布局中多個(gè)控件同時(shí)使用一個(gè)資源的時(shí)候,這些控件會(huì)共用一個(gè)狀態(tài) // 如果你改變了一個(gè)控件的狀態(tài),其他的控件都會(huì)接收到相同的通知 title.getBackground().setAlpha(51); search.getBackground().setAlpha(153); } public void button03(View view){ // 使用mutate()方法使該控件狀態(tài)不定,這樣不定狀態(tài)的控件就不會(huì)共享自己的狀態(tài)了 title.getBackground().mutate().setAlpha(51); search.getBackground().mutate().setAlpha(153); }
布局:
<?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"> <LinearLayout android:id="@+id/ll_title" android:layout_width="match_parent" android:layout_height="80dp" android:gravity="center" android:background="#0000ff" android:orientation="horizontal"> <EditText android:id="@+id/et_search" android:layout_width="200dp" android:layout_height="60dp" android:gravity="center" android:hint="輸入框" android:textColorHint="#ffffff" android:background="@drawable/search_title_bg"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="01" android:onClick="button01"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="02" android:onClick="button02"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="03" android:onClick="button03"/> </LinearLayout> </LinearLayout>
輸入框背景 search_title_bg
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#000000"/> <corners android:radius="8dp"/> <stroke android:width="1dp" android:color="#666666"/> </shape>
四、寫在后面的
背景透明度設(shè)置比較常見,mutate()方法,可以解決背景透明狀態(tài)設(shè)置異常的現(xiàn)象。
相關(guān)文章
Android實(shí)現(xiàn)圓線按鈕進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓線按鈕帶進(jìn)度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Android自定義view Path 的高級用法之搜索按鈕動(dòng)畫
這篇文章主要介紹了Android自定義view Path 的高級用法之搜索按鈕動(dòng)畫,需要的朋友可以參考下2017-06-06Android實(shí)現(xiàn)水波紋擴(kuò)散效果的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)水波紋擴(kuò)散效果的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Android利用FlexboxLayout輕松實(shí)現(xiàn)流動(dòng)布局
flexbox是屬于CSS的一種布局方案,可以簡單、完整、響應(yīng)式的實(shí)現(xiàn)各種頁面布局。谷歌將其引入以提高復(fù)雜布局的能力。下面這篇文章主要給大家介紹了在Android中利用FlexboxLayout輕松實(shí)現(xiàn)流動(dòng)布局的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04Android activity實(shí)現(xiàn)延時(shí)跳轉(zhuǎn)功能
Activity是一個(gè)Android的應(yīng)用組件,它提供屏幕進(jìn)行交互。今天通過本文給大家介紹Android activity實(shí)現(xiàn)延時(shí)跳轉(zhuǎn)功能,感興趣的朋友一起看看吧2021-06-06android應(yīng)用實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)方法
這篇文章主要介紹了android應(yīng)用實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)方法,本文講解了原理和編碼實(shí)例,需要的朋友可以參考下2015-05-05Flutter狀態(tài)管理Bloc之定時(shí)器示例
這篇文章主要為大家詳細(xì)介紹了Flutter狀態(tài)管理Bloc之定時(shí)器示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03