【问题标题】:Migrate from ActionBar to ToolBar从 ActionBar 迁移到 ToolBar
【发布时间】:2015-04-04 15:40:19
【问题描述】:

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

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

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

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

也就是说,我怎样才能使用新的Toolbar 实现相同的行为?

【问题讨论】:

    标签: android compatibility material-design android-toolbar


    【解决方案1】:

    只需查看android developers blog。它包含“如何”的解释

    很快

    View 一样将 Toolbar 添加到您的活动布局中

    <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 设置为 ActionBar

    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>
    

    【讨论】:

      猜你喜欢
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多