Android - toolbar navigation 樣式

來源:互聯網
上載者:User

標籤:ide   重載   scene   esc   add   ima   wrap   xmlns   man   

1.修改title 邊距

修改邊距使用系統的app屬性來引入使用,即:

  xmlns:app="http://schemas.android.com/apk/res-auto"
  • 1

比如:

<android.support.v7.widget.Toolbar    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="1dp" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" android:orientation="vertical" app:contentInsetLeft="10dp"/>

 

當然也可以在style.xml中修改,自己研究吧;

2.修改navigation icon的padding值

修改padding值,就需要在style.xml中修改;在此我們修改的是navigation的pading值:

Widget.AppCompat.Toolbar.Button.Navigation

 

比如:

1.定義style

    <style name="myToolbarNavigationButtonStyle" parent="@style/Widget.AppCompat.Toolbar.Button.Navigation"> <item name="android:minWidth">0dp</item> <item name="android:padding">@dimen/margin_horizontal_large</item> <item name="android:scaleType">centerInside</item> </style>
  • 1

2.app主題中應用

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="toolbarNavigationButtonStyle">@style/myToolbarNavigationButtonStyle</item> </style>
  • 1
3.title置中

toolbar是可以自訂布局的,可以在toolbar中添加一個textview來實現,從而代替title;
比如:

1.布局

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar    android:id="@+id/toolbar" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"? android:layout_height="?actionBarSize" android:background="@null" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/AppTheme"> <TextView android:id="@+id/toolbar_title" style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/></android.support.v7.widget.Toolbar>

 

2.初始化

View v = findViewById(R.id.toolbar);if (v != null) {    toolbar = (Toolbar) v;    setSupportActionBar(toolbar);    toolbarTitle = (TextView) v.findViewById(R.id.toolbar_title);    if (toolbarTitle != null) {        getSupportActionBar().setDisplayShowTitleEnabled(false); }}

 

3.使用
設定title兩種方式:
(1)直接在setText();
(2)在AndroidManifest.xml中指定title;
(3)如果有baseActivity的話,解決如下:
Activity有一個onTitleChanged的介面,在Activity的onPostCreate與setTitle中都會調用這個方法;

protected void onPostCreate(@Nullable Bundle savedInstanceState) {    if (!isChild()) {        mTitleReady = true; onTitleChanged(getTitle(), getTitleColor()); } mCalled = true;}

 

所以只需要在BaseActivity中重載這個方法就行了,如下所示 :

@Overrideprotected void onTitleChanged(CharSequence title, int color) { super.onTitleChanged(title, color); if (toolbarTitle != null) { toolbarTitle.setText(title); }}

 

參考資料 - 點我

4.修改menu的padding值

定義style值:

    <style name="mToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">        <item name="android:paddingRight">0dp</item> <item name="android:paddingLeft">13dp</item> </style>
  • 1

使用:

<android.support.v7.widget.Toolbar    android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="1dp" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" android:orientation="vertical" style="@style/mToolbarStyle"/>
  • 1

上面的方法,在有些機子上會失效,大家有上面辦法,請告訴我,謝謝!

5.參考

修改toolbar的一些屬性
toolbar學習使用

Android - toolbar navigation 樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.