为什么在使用ActionBar时AppCompat中的工具栏不显示?

时间:2014-11-22 12:30:24

标签: android android-actionbar navigation-drawer android-appcompat android-toolbar

Google在他们的Android博客中提到,如果想要使用Toolbar使用AppCompat,则有两种可能性。一种是将它用作ActionBar,另一种是单独使用它,但是当我使用ActionBarDrawerToggle时,我需要第一个选项。所以我必须扩展ActionBarActivity,我的主题必须继承Theme.Appcompat(或其中一个变体)。此外,要使用第一个选项,我必须继承NoActionBar变体。最后,我不得不打电话给setSupportActionBar(mToolbar),我做了。但是,ActionBar仍然没有显示。以下是我的代码。

我已经看过this post了,但我想我的情况是SwipeRefreshLayout会毁了它。我尝试了不同的地方来包含工具栏,但它们都不起作用。如何在不破坏其余布局的情况下包含工具栏?感谢。

MainActivity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_orders_list);

    context = getApplicationContext();


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


    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.refreshable_content);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorSchemeResources(R.color.refresh_one,
            R.color.refresh_four, 
            R.color.sun_flower, 
            R.color.carrot);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    getSupportActionBar().setTitle(R.string.title_activity_orders_list);

    drawerToggleLeft = new ActionBarDrawerToggle(this, drawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close)
    {
        @Override
        public void onDrawerClosed(View view) 
        {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(R.string.title_activity_orders_list);
            findViewById(R.id.action_add_order).setVisibility(View.VISIBLE);
            invalidateOptionsMenu();
            syncState();
        }

        @Override
        public void onDrawerOpened(View drawerView) 
        {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle("");
            findViewById(R.id.action_add_order).setVisibility(View.INVISIBLE);
            invalidateOptionsMenu();
            syncState();
        }



    };



    drawerLayout.setDrawerListener(drawerToggleLeft);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    drawerToggleLeft.syncState();


    ordersList = (ListView) findViewById(android.R.id.list);
}

theme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources >

    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/turquoise</item>
        <item name="colorPrimaryDark">@color/green_sea</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>


    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@color/white</item>
    </style>

</resources>

custom_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/turquoise" />

activity_order_list.xml

<android.support.v4.widget.DrawerLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <include layout="@layout/custom_toolbar" /> // here is the toolbar

    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".OrdersListActivity"
        android:background="@color/white" 
        android:id="@+id/refreshable_content">



        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:cacheColorHint="@android:color/transparent"
            android:divider="@null"> 
        </ListView>

    </android.support.v4.widget.SwipeRefreshLayout>
    <LinearLayout
        android:id="@+id/drawer_left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:orientation="vertical">

        <include layout="@layout/custom_toolbar" /> // here is the toolbar

        <fragment
            android:id="@+id/drawer_fragment_left"
            android:name="com.ordr.view.DrawerFragmentLeft"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

      </LinearLayout>
</android.support.v4.widget.DrawerLayout>

0 个答案:

没有答案