android 应用程序中未显示工具栏

Toolbar is not shown in android application

我最近开始将我的旧应用程序迁移到 material 设计,并决定使用工具栏而不是 actionbar 更多 customization.I 尝试将工具栏添加到只有一个 activity 与以下代码:

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize" >
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:background="@drawable/background_tabs" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabs"
        tools:context=".MainActivity" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private MyPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApp.tracker().send(new HitBuilders.EventBuilder("ui", "open")
        .setLabel("main")
        .build());
        // Set a ToolBar to replace the ActionBar.
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
}

}

style.xml

<style name="MyTheme.Base" parent="@style/Theme.AppCompat.Light.NoActionBar">

        <!-- your app branding color for the app bar -->
        <item name="colorPrimary">@color/redLatest</item>
        <!-- darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">@color/redLatest_dark</item>
        <item name="colorAccent">@color/redLatest</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="actionDropDownStyle">@style/MyActionBarDropDown</item>
    </style>

现在,当我 运行 上面的代码时,我的 activity 中没有操作栏,这很好,但工具栏也没有显示,这很奇怪。我知道我可能犯了一些我找不到的愚蠢错误 out.Please help

您已经在 RelativeLayout 中添加了视图,因此您应该指定它们必须放置的位置。由于您没有为 TabLayout 指定它,因此 TablayoutToolbar 重叠。只需在 TabLayout.

中添加 android:layout_below="@id/toolbar"
<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_below="@id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:background="@drawable/background_tabs" />

使用这个

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize" >
</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_below="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:background="@drawable/background_tabs" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/tabs"
    tools:context=".MainActivity" />