从ActionBar迁移到ToolBar

vin*_*ius 2 compatibility android material-design android-toolbar

我需要升级要使用的应用Material Design.我要做的第一件事是展示新的Toolbar,但我不确定我应该怎么做.

我有我的主要FragmentActivity,我膨胀了一些menu并做:

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.main_color)));
Run Code Online (Sandbox Code Playgroud)

我也用NavigationDrawer,所以我也为抽屉设置了一个图标.

说,如何使用新的实现相同的行为Toolbar

Kir*_*nov 8

只看一个Android开发者博客.它包含"如何"的解释

不久

添加工具栏就像一个浏览到您的活动的布局

<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" />
Run Code Online (Sandbox Code Playgroud)

您应该将工具栏设置为ActionBar

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)

不要忘记造型.将新的style.xml添加到资源目录值-21v,之后您的工具栏将由colorPrimary着色

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name=”colorPrimary”>@color/my_awesome_color</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name=”colorPrimaryDark”>@color/my_awesome_darker_color</item>

    <!-- colorAccent is used as the default value for colorControlActivated,
     which is used to tint widgets -->
    <item name=”colorAccent”>@color/accent</item>

    <!-- You can also set colorControlNormal, colorControlActivated
     colorControlHighlight, and colorSwitchThumbNormal. -->

</style>
Run Code Online (Sandbox Code Playgroud)