从ActionBar迁移到ToolBar

3

我需要将一个应用程序升级以使用Material Design。我正在尝试做的第一件事是显示新的工具栏,但我不确定该如何做。

我有一个主要的FragmentActivity,其中我填充了一些menu并执行以下操作:

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.main_color)));

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

话说回来,我如何使用新的Toolbar实现相同的行为呢?

1个回答

7

请看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" />

您应将您的工具栏设置为一个操作栏
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

不要忘记样式。将新的style.xml添加到资源目录values-21v中,这样您的Toolbar将被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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接