Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

android - Why does Toolbar in AppCompat not show when using as an ActionBar?

Google mentioned in their Android blog that there are two possibilities if one wants to use the new Toolbar using AppCompat. One is by using it as an ActionBar and one using it standalone, but as I am using ActionBarDrawerToggle, I need the first option. So I have to extend ActionBarActivity and my theme must inherit Theme.Appcompat (or one of it's variations). Also, to use the first option I had to inherit the NoActionBar variation. And finally, I had to call setSupportActionBar(mToolbar), which I did. However, the ActionBar still does not show. Below is my code.

I have already seen this post, but I guess in my case it's SwipeRefreshLayout that ruins it. I tried different places to include the toolbar, but none of them works. How can I include the toolbar without ruining the rest of the layout? Thanks.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...