尝试使用支持工具栏,但它不起作用



这是我的布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/drawer_layout"
          tools:ignore="MergeRootFrame">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/toolbar"/>
    <android.support.v4.view.ViewPager
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/pager">
        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary"
            android:textColor="@color/colorSecondary"
            android:paddingTop="4dp"
            android:paddingBottom="4dp" />
    </android.support.v4.view.ViewPager>
</LinearLayout>
<ListView android:id="@+id/left_drawer"
          android:layout_width="320dp"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:choiceMode="singleChoice"
          android:divider="@android:color/transparent"
          android:dividerHeight="0dp"
          android:background="#111"/>

相关代码片段:

mToolbar = (Toolbar) findViewById(R.id.toolbar);
if(mToolbar != null) {
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mToolbar.setTitle("Some title");
}

标题永远不会显示。我没有收到任何错误,我可以在 mToolbar 上进行调用,就好像它正在工作一样。我也在我的应用程序顶部看到它,所以它就在那里,它没有被其他任何东西覆盖,但文本没有更新,我无法设置任何图标,我对它所做的一切似乎都不起作用。

我不知道该怎么做,处理工具栏的教程使它看起来非常简单,但它只是不起作用。

编辑:这是工具栏.xml

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="120dp"
android:theme="@style/Base.Widget.AppCompat.Toolbar"
android:background="?attr/colorPrimary"/>

事实证明,我从我的基本活动和主活动中调用了setContentView,似乎一旦第二次调用它,从第一次调用创建的视图将是孤立的,不再起作用。

遵循工具栏教程之前,我不需要在基本活动中弄乱我的布局,所以这就是为什么我突然调用setContentView两次。

最新更新