1. 程式人生 > >Android Material Design > Toolbar

Android Material Design > Toolbar

 水很深 很深   

 看註釋  theme  / dark  light


  <!--https://www.jianshu.com/p/882f8ba1e43b  設定黑白圖示 字型-->
    <!--contentInsetStart用來設定左邊偏移-->
    <!--subtitle 的 顏色  @style/Toolbar.Subtitle 這個裡邊設定-->

    <android.support.v7.widget.Toolbar
        android:id="@+id/mytoolbar"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:elevation="4dp"



        app:contentInsetStart="0dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/AppTheme.PopupOverlay"

        app:logo="@mipmap/ic_launcher"

        app:subtitle="subtitle"
        app:title="title"

        app:titleTextColor="@color/colorAccent"
        app:subtitleTextColor="@color/colorAccent"

        app:titleTextAppearance="@style/Toolbar.Title"
        app:subtitleTextAppearance="@style/Toolbar.Subtitle"
        />

//https://me.csdn.net/qq_30447263
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.mytoolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//隱藏  顯示 箭頭


//        getSupportActionBar().setTitle("SSSSS");//設定title  這裡建議直接在XML 裡邊 設定title  可以直接使用contentInsetStart 居中
//        getSupportActionBar().setDisplayShowTitleEnabled(true);//隱藏  顯示title


//        toolbar.setNavigationIcon(R.mipmap.icon_see);//設定對應的圖片
//        app:navigationIcon="@drawable/icon_a"    xml


//        Fragment中 setHasOptionsMenu(true);  ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);  onCreateOptionsMenu
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_tool_bar, menu);//載入menu佈局
        return true;
    }

    /**
     * 由於google 不在支援menu 中顯示圖示 所以 就這樣
     * https://blog.csdn.net/shangming150/article/details/77914110
     *
     * @param featureId
     * @param menu
     * @return
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

    //設定點選事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.tab_one:
                Toast.makeText(this, "tab_one", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tab_two:
                Toast.makeText(this, "tab_two", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tab_three:
                Toast.makeText(this, "tab_three", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tab_four:
                Toast.makeText(this, "tab_four", Toast.LENGTH_SHORT).show();
                break;

            case android.R.id.home:
                Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
                break;

        }
        return super.onOptionsItemSelected(item);
    }
}

styles

<resources xmlns:tools="http://schemas.android.com/tools">

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

        <!-- 新增這一句, 即可改變toolbar menu 圖示的顏色-->
        <item name="android:textColorSecondary">@color/colorAccent</item>
        <!--toolbar標題文字顏色-->
        <item name="android:textColorPrimary">@color/colorAccent</item>
    </style>


    <!--Toolbar需要的-->
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
        <!-- 是否覆蓋錨點,預設為true,即蓋住Toolbar -->
        <item name="overlapAnchor">false</item>
        <!--距離右邊間距-->
        <item name="android:paddingRight">0dp</item>
        <!--彈出選單的背景顏色-->
        <item name="android:colorBackground">#0f0</item>
        <!--彈出選單的字型顏色-->
        <item name="android:textColorPrimary">#f00</item>


    </style>

    <!--  定製subtitle  字型大小 顏色 -->
    <style name="Toolbar.Subtitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">#f0f</item>
    </style>

    <!--  定製title  字型大小 顏色 -->
    <style name="Toolbar.Title" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">#f0f</item>
    </style>
</resources>