Android手动添加工具栏与默认主题工具栏

Android手动添加工具栏与默认主题工具栏,android,android-toolbar,Android,Android Toolbar,在所有的例子中,我看到人们用 <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="colorPrimary">

在所有的例子中,我看到人们用

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
为什么这比只使用主题附带的默认ActionBar要好?由于采用这种方式,我必须在应用程序中的每个活动中手动添加它,同时使用默认值,它可以自己添加吗?另外,使用主题中的工具栏,我现在可以将我的主要活动\u layout.xml缩减为

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main.MainActivity">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="fixed"
    app:tabGravity="fill"/>


<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_below="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

</RelativeLayout>

不一定要使用工具栏,但如果你想让你的应用程序遵循材质设计准则,最好使用它。而且

  • 如果您想向它添加一个小部件,您可以像在任何其他布局中一样轻松地在其中放置项目
  • 您可以将其放置在布局中的任何位置
  • 您可以创建任意数量的工具栏

实际上还有更多我没有提到的原因。

由于
工具栏
s是常规的
视图
s,它们更容易根据需要进行修改和定制。如果提供的
ActionBar
满足您的所有需求,您可以使用它。我可以使用.getActionBar()访问“默认”工具栏,对吗?所以一旦我有了这个,我可以做任何我喜欢的事。人们这样做一定有原因……老实说,大多数人这样做的原因是因为他们盲目地从网上看到的示例中复制代码。正如我所说的,
工具栏
更容易修改,许多与
ActionBar
模式相关的东西都被弃用了。使用第二个XML,您改变了布局的行为。当您垂直滚动时,
CoordinatorLayout
将负责转换屏幕的
工具栏
。这在默认的
操作栏中是不可能的。我不会在任何地方垂直滚动。我只使用ViewPager向侧面滚动。另外,这只是我在看的一个例子。它背后的要点是展示人们如何使用**windowActionBar**和windowNoTitle主题属性删除ActionBar,但稍后在layout.xml和java代码中手动添加它。我正在寻找这背后的原因,为什么更可取。
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main.MainActivity">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="fixed"
    app:tabGravity="fill"/>


<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_below="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

</RelativeLayout>