Android 背景透明度設(shè)置總結(jié)
一、寫在前面的
在需求上遇到背景設(shè)置透明度還是比較常見的,設(shè)置透明度有幾種方式,但是不同的場(chǎng)景應(yīng)用下,不同的方式可能會(huì)出現(xiàn)一些問(wèn)題。針對(duì)開發(fā)過(guò)程中的需求做以下總結(jié)。
二、先看效果圖
圖1、
圖2、

圖3、
圖4

介紹:圖1、藍(lán)色頭部和輸入框背景初始狀態(tài)
圖2、點(diǎn)擊按鈕01,輸入框的透明度不起作用,和title的透明度一樣
圖3、點(diǎn)擊按鈕02,背景透明度設(shè)置正常,但是可能會(huì)對(duì)全局的背景有影響
圖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)文章
AndroidStudio重新share代碼和上傳到svn新地址教程
這篇文章主要介紹了AndroidStudio重新share代碼和上傳到svn新地址教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
9個(gè)非常棒的Android代碼編輯器 移動(dòng)開發(fā)者的最愛
這篇文章主要為大家分享了9個(gè)非常棒的Android代碼編輯器,據(jù)說(shuō)這可是移動(dòng)開發(fā)者的最愛,知道是哪九個(gè)Android代碼編輯器2015-12-12
Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果
這篇文章主要為大家詳細(xì)介紹了Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Android?雙屏異顯自適應(yīng)Dialog的實(shí)現(xiàn)
Android 多屏互聯(lián)的時(shí)代,必然會(huì)出現(xiàn)多屏連接的問(wèn)題,本文主要介紹了Android?雙屏異顯自適應(yīng)Dialog的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Android 對(duì)話框(Dialog)大全詳解及示例代碼
本文主要介紹Android 對(duì)話框的知識(shí),這里整理了詳細(xì)資料及實(shí)現(xiàn)示例代碼及實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-09-09
Android中引用其他程序的文本資源超簡(jiǎn)單方法
這篇文章主要介紹了Android中引用其他程序的文本資源超簡(jiǎn)單方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-01-01
Android實(shí)現(xiàn)水波紋點(diǎn)擊效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)水波紋點(diǎn)擊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android開發(fā)中優(yōu)秀的app 異常處理機(jī)制
這篇文章主要介紹了Android開發(fā)中優(yōu)秀的app 異常處理機(jī)制 的相關(guān)資料,需要的朋友可以參考下2015-12-12

