[英]Android - Is this not possible to change the color of search icon in ActionBar?
我在我的android項目中使用默認的ActionBar
。 我正在使用Theme.AppCompat.Light.DarkActionBar
主題。
我創建XML
文件menu
和search item
從這個menu
下面是:
<item
android:id="@+id/search_button"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
app:showAsAction="always" />
當我在物理設備上加載應用程序以測試search icon
的顏色不是白色時,它是一種帶紅色的顏色( ActionBar
顏色ActionBar
),請參見下圖:
標題顏色是白色,可以,溢出菜單按鈕顏色是白色,可以,但是搜索圖標按鈕不是白色。 因此,我進行了研究,發現以下沒有人參與的資源。
最終,我意識到無法更改ActionBar
默認搜索圖標的顏色。 我只想讓這個問題成為該問題的最后且長期的解釋,涵蓋了Why the icon color is different then the title color and overflow menu icon color?
, Why this thing is impossible to solve?
, Where this color property of xml for search icon is hidden that is hard to find?
以及How(if possible) to change the color of this default search icon in the ActionBar
? 我知道我們可以在此處添加帶有白色的自定義圖標,我們也有ToolBar
,但是我不希望這種解決方案,因為如果可以更改ActionBar
的標題,則將溢出菜單選項添加到ActionBar
(等),然后也應該可以在ActionBar
更改搜索圖標的顏色。 可能是我找到了一個知道這個問題的隱藏怪胎,並且可以解釋Why
和What
使這個問題成為未來android的最后一個有用的信息/解決方案。
如果有人想給我的問題打分,我很樂意為此做准備,但至少這個問題對將來的android新手很有幫助。 謝謝!!!
為什么圖標顏色與標題顏色和溢出菜單圖標顏色不同?
@android:drawable/ic_menu_search
(您可以在Android\\Sdk\\platforms\\android-28\\data\\res\\drawable
找到它們)是一個帶有一些透明白色的png文件(這就是為什么要獲得白色紅色的原因)
為什么這件事無法解決?
這並非不可能,您可以按照下面的答案創建自己的圖標。
隱藏搜索XML的xml顏色屬性的地方很難找到?
沒有xml。 只是png文件。 取決於主題,android將選擇其中之一。(例如,對於“輕動作”欄為黑色圖標,對於“暗行動”欄為白色圖標)
如何(如果可能)在ActionBar中更改此默認搜索圖標的顏色?
解決方案是為搜索圖標創建自己的矢量圖像。 您可以分配所需的任何顏色。 然后在菜單中使用此圖標。
第1步:創建矢量圖像
右鍵單擊drawable
文件夾->新建->矢量資產
第2步:創建搜索圖標並選擇顏色
更改Clipart
和color
步驟3:在選單中使用這個圖示
<item
android:id="@+id/search_button"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="always" />
在矢量可繪制下方使用
ic_search.xml (res / drawable / ic_search.xml)
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#FFFFFF"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z" />
</vector>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.