工具栏显示一个空盒子

时间:2015-12-22 11:05:06

标签: android toolbar android-support-library

我之前从未使用过工具栏,但从这个新项目开始我想尝试一下。但是我已经遇到了麻烦。 据我所知,我使用appcompact工具栏来定义布局中的工具栏:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">
    <!--toolbar-->
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/toolBar"
        android:layout_width="match_parent"
        android:layout_height="64dp">
    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/toolbar"
        android:id="@+id/container"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </LinearLayout>
</RelativeLayout>

另外,在我的活动中,我使用getSupportActionBar()

进行设置
protected void setupActionBar() {
    // Set a toolbar to replace the action bar.
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("My custom toolbar!");
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        logger.debug("Toolbar Setted");
    }
}

但是结果只显示一个没有标题或功能的空盒子。 我不明白我错过了什么,这个过程应该非常简单。 我使用的样式文件如下:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item>
</style>

1 个答案:

答案 0 :(得分:0)

在布局文件夹中创建toolbar.xml文件并将此代码放入其中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="56dp"
    android:background="@color/colorPrimary"
    android:id="@+id/toolBar" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- Place what ever you want in your layout -->

    </FrameLayout>

</android.support.v7.widget.Toolbar>

在此活动布局中添加此工具栏,如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="@android:color/white"
    tools:context=".MainActivity">

    <include layout="@layout/toolbar"></include>

    <!-- Create your main layout here -->

</LinearLayout>

在您的活动中,您可以像这样设置工具栏

Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);