導航:首頁 > 操作系統 > androidtoolbar按鈕顏色

androidtoolbar按鈕顏色

發布時間:2023-02-08 02:17:49

1. 如何設置android 5.0主題,狀態欄,toolbar顏色設定

關鍵圖:

關鍵代碼:

res->values->styles

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

2. 怎麼給Toolbar設置顏色

在桌面上單擊右鍵。單擊個性化。點擊「窗口顏色」。更改顏色後單擊「保存修改」即可。

3. Android Toolbar的詳細使用步驟

1.首先在 build.gradle 引入support v7包

2.找到 Manifest 文件的 Application 標簽下 Theme 屬性

3.自定義 Theme 屬性,因為 Activity 默認是有 ActionBar 的,所以需要先將默認的 ActionBar 去掉( parent="Theme.AppCompat.Light.NoActionBar" ),並根據項目需求選擇主題的每個特定的屬性

附錄一張常用屬性圖,上面的每個屬性就很好理解了。

1.先在需要添加 Toolbar 的 xml 文件中,加入 Toolbar 控制項

解釋一下 Toolbar 裡面的屬性,這裡面的屬性大多見名知意,很好理解。
我們發現 Toolbar 裡面有三個屬性是以 app 作為前綴,因為 Toolbar 在 5.0 系統才出現,以 app 為前綴名的屬性是為了兼容 5.0 以下的系統 。
咱們一個個分析,先講下這個屬性

這個根據項目需求,我們自定義的 Toolbar 屬性。關鍵點:因為我們 App 的主題是淺色的主題 "Theme.AppCompat.Light.NoActionBar" ,所以 Toolbar 中,我們繼承了 parent="ThemeOverlay.AppCompat.Dark.ActionBar" , 如果 Toolbar 不用深色的主題,標題欄的字體之類看不清楚。

見名知意,這是 PopWindow 的主題,由於我們 ActionBar 設置的是深色的,默認情況下, PopWindow 和 ActionBar 的主題是統一的,但 PopWindow 的深色主題和整個 App 的整體顏色風格太不搭,所以我們需要將主題改成和 App 風格一致。

以上屬性都是根據項目需求設定的,可加可不加, Toolbar 的可定製性很強~

這樣我們就做到了,隱藏 ActionBar,使用 Toolbar 了。 注意:標題欄是默認在左上角的,並沒有居中

1.先自定義標題欄,讓標題居中

2.在 Toolbar 上添加幾個按鈕,先在 res 目錄下新建一個文件夾 Menu ,創建一個 toolbar_menu.xml 文件

在MainActivity 重寫 onCreateOptionsMenu 、onOptionsItemSelected 方法

4. Android狀態欄著色(非沉浸式狀態欄)

由於產品需求 , 要將一級頁面的5個Fragment設置不一樣顏色的狀態欄; 然後我對著網上的博文抄了一遍 ,發現兼容問題很差 , 要麼就是頂部的狀態欄沒有實現全屏 , 要麼就是底部的虛擬按鈕遮擋導航欄(華為手機和小米手機), 最後自定義主題和設置xml布局頂部高度來解決了這個問題;
解決方案:

第一步.在values的styles中自定義主題:

第二步.創建values-v19文件夾和styles.xml:

第三步.創建values-v21文件夾和styles.xml:

第三步.引用主題:

第四步.Fragment 狀態欄填充顏色:(如果一節界面的頭部是輪播圖直接預留20dp的高度)
不設置預留高度,狀態欄會和toolbar疊加在一起 (狀態欄在toolbar上方)

運行後效果圖如下:

輪播圖在頂部的時候直接預留狀態欄的高度即可:

指定顏色的界面設置多一層有顏色的View:

5. 如何使用 SearchView 工具欄中Android

眾所周知,在使用ActionBar的時候,一堆的問題:這個文字能不能定製,位置能不能改變,圖標的間距怎麼控制神馬的,由此暴露出了ActionBar設計的不靈活。為此官方提供了ToolBar,並且提供了supprot library用於向下兼容。Toolbar之所以靈活,是因為它其實就是一個ViewGroup,我們在使用的時候和普通的組件一樣,在布局文件中聲明。
Part1:ToolBar的引入step1:設置style主題,主要任務是去除原本的ActionBar

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"><!--Light.DarkActionBar表示默認的黑色主體的Actionbar-->
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/sky_blue</item>
<item name="colorPrimaryDark">@color/deep_blue</item>
<item name="colorAccent">@color/material_deep_teal_200</item>
<item name="android:textColorPrimary">@color/white</item>
</style>
colorPrimary表示標題欄ActionBar的顏色;colorPrimaryDark表示狀態欄的顏色; colorAccent表示輸入框,按鈕等被選中時的顏色; textColorPrimary表示標題欄(ActionBar或者ToolBar)中字體的顏色
當然啦,第一步的實現也可以在程序代碼中或者style里靜態或者動態地去掉ActionBar
step2:在你需要引入ToolBar的布局文件中引入ToolBar:

<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark"//這里的主題可以用來反襯toolBar的overFlow顏色
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/sky_blue"
></android.support.v7.widget.Toolbar>
因為colorPrimary是給ActionBar設定顏色的,因此如果我們使用ToolBar,就需要自己去設定ToolBar的背景顏色
setp3:在代碼中獲取ToolBar控制項以及實例化:

Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
toolbar.setNavigationIcon(R.mipmap.ic_toc_white_24dp);//設置ToolBar頭部圖標
toolbar.setTitle("ToolBar");//設置標題,也可以在xml中靜態實現
setSupportActionBar(toolbar);//使活動支持ToolBar
Part2:ToolBar里各SearchView的引入:
setp1:在menu/xxxx.xml的菜單布局文件將SearchView以菜單條目的方式加入到ToolBar中

<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"
android:inputType="textCapWords"
android:imeOptions="actionSearch"
android:title="search" />
這中添加條目的方式和添加普通控制項的方式是一樣的,這里需要對showAsAction具體說明以下:
ifRoom表示當toolBar空間足夠時,顯示圖標在標題欄中,否則將它隱藏到ToolBar末端的overFlow中,點開overFlow只顯示item的title
CollapseActionView表示當前空間點開之後占據整個ToolBar空間
always表示總是顯示在標題欄中,當我們長按該item後,就會以Toast的方式顯示出它的title
never表示總是隱藏在overFlow中
step2:在Java程序代碼中實例SearchView
覆寫onCreateOptionsMenu方法,為什麼要在這個方法里實現對SearchView的實例化呢?因為toolBar里的點擊事件都以菜單的形式實現的,如果我們需要讓它隱藏到overFlow中,並且點擊菜單鍵並喚出,那麼就只需要把它的顯示方式設置為never即可。

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);//在菜單中找到對應控制項的item
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
Log.d("Tag", "menu create");
MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {//設置打開關閉動作監聽
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(MainActivity.this, "onExpand", Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(MainActivity.this, "Collapse", Toast.LENGTH_LONG).show();
return true;
}
});
return super.onCreateOptionsMenu(menu);

6. 如何設置android 5.0主題,狀態欄,toolbar顏色設定

默認的手機都是不能設置的,只能設置主背景和一些壁紙。建議下載一些徐該主題的軟體,到應用商店上搜搜看看,有很多額。

7. android v7包里的Toolbar,怎麼定製圖標,字體居中的效果

1.文字的話僅可設置為底部居中或中部居右,在TextAlignment屬性中設置,值分別為0和1,沒有中部居中,至於為什麼在下面說明了; 2.不能改字體,不能改顏色。 另外,強烈建議用Toolbar工具欄設計時使用圖標來代替文字,或者圖標和文字都有,相信用過Windows我的電腦工具欄自定義的都知道,標簽可選為「顯示文本標簽」(就是顯示在圖標下面)或「選擇性地文本置於右側」這就是第1點為什麼只能選2個值的原因了。 至於怎麼用圖標,再拖一個ImageList控制項進窗體,設計時插入所有要用到的圖標,記住每個圖標的索引編號,在Toolbar控制項中設置按鈕圖像為索引編號,0為沒有圖標。 編程時實現採用 Toolbar1.Buttons(1).Image = 索引

8. 如何設置toolbar里的DrawerLayout按鈕的顏色

首先使用 Toolbar 來代替ActionBar
,這樣我們就能夠把ActionBar嵌入到我們的View體系中,然後我們"禁用"系統的status bar,由 DrawerLayout
來處理status bar,最後抽屜部分往上移,或者裁剪掉status bar那一部分。
控制Status bar
在你的values-v21裡面添加新的主題,並設置一下屬性:
values-v21/themes.xml
<style name="AppTheme">
<item name="android:">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>

這里解釋一下:

,將它設置為true,系統將在你的window裡面繪制status
bar,默認為 TRUE
,之所以要寫出來是因為你的theme有可能是繼承過來的,確保為true。(在這里小插曲一下,因調試時,總以為注釋了這段代碼就以為是false,程
序員思維害苦了我。另外從命名來看,Android把它稱為system bar,可能是為了與能被我們處理的status bar區分開而做的改變。)
statusBarColor 設置為透明是因為我們不再需要系統的status bar,因為我們無法控制它的位置,後面我們將交由 DrawerLayout 來處理。
使用DrawerLayout
首先,你的布局文件應該是和這個類似的:
<android.support.v4.widget.DrawerLayout
xmlns:android="url"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- The rest of your content view -->
</LinearLayout>
<!-- The navigation drawer -->
<ScrimInsetsFrameLayout xmlns:android="rul"
xmlns:app="url"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/white"
android:elevation="10dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000">
<!-- Your drawer content -->
</ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>


在這裡布局裡面我們用到了一個的開源類 ScrimInsetsFrameLayout ,它的主要作用就是利用 fitsSystemWindows
的回調方法 fitSystemWindows(Rect insets) 來獲取status bar的大小,然後調整畫布已達到去掉status
bar的效果,所以我們需要在ScrimInsetsFrameLayout 下設置 fitsSystemWindows
為true。當然你也可以不使用這個類,而改用 layout_marginTop 屬性來達到效果。
insetForeground 這個屬性是ScrimInsetsFrameLayout自帶的,表示插入區域的前景色,我們設置為帶透明的黑色#4000。別忘了使用這個屬性需要添加如下代碼到attrs.xml里:
values/attrs.xml
<declare-styleable name="ScrimInsetsView">
<attr name="insetForeground" format="reference|color" />
</declare-styleable>


自此,我們已經實現了將DrawerLayout抽屜的那一部分顯示在 Toolbar 和systembar(為了和下面的status
bar區分,我們稱為system bar)之間了,可是system bar的顏色被我們設置了透明,所以我們接下來要改變status
bar的顏色。
改變Status bar的顏色
你可能已經注意到剛才的布局裡面 DrawerLayout 的 fitsSystemWindows 屬性設置了為true,這是因為我們要在代碼裡面使用了 DrawerLayout 設置status bar顏色的方法:
// 在這里我們獲取了主題暗色,並設置了status bar的顏色
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
int color = typedValue.data;
// 注意setStatusBarBackgroundColor方法需要你將fitsSystemWindows設置為true才會生效
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
drawerLayout.setStatusBarBackgroundColor(color);

使用ToolBar來代替ActionBar

在代碼裡面這樣設置:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

閱讀全文

與androidtoolbar按鈕顏色相關的資料

熱點內容
hbuilderx文件夾有哪些 瀏覽:102
空調壓縮機生產板塊 瀏覽:612
開源多媒體伺服器都有什麼 瀏覽:392
反編譯了別人的app會被發現嗎 瀏覽:918
上海光裕汽車壓縮機有限公司 瀏覽:333
連接ps4伺服器地址 瀏覽:136
新神魔大陸三星賬號是什麼伺服器 瀏覽:677
壓縮機lj100cy 瀏覽:556
王者系統怎麼轉回安卓系統 瀏覽:749
linux查看路由表命令 瀏覽:506
高手程序員使用什麼筆記本 瀏覽:440
ios壓縮圖片app 瀏覽:839
排隊論pdf 瀏覽:520
python調用無參函數 瀏覽:799
主管開除女程序員 瀏覽:713
雲伺服器轉售 瀏覽:541
壓縮空氣漏氣量怎樣計算 瀏覽:103
手機app是怎麼跳轉的 瀏覽:664
學編程的重要性 瀏覽:25
程序員去按摩 瀏覽:740