Android工具栏在顶部状态栏后面推升

时间:2015-03-30 19:05:43

标签: android android-toolbar

我最近将手机更新为Android 5.0.2,我正在开发的应用程序中有一个闪烁的工具栏。工具栏被向上推,并被最顶部的状态栏部分隐藏。

会导致此问题的原因是什么?

以下是具有处理工具栏

布局的Activity的代码
public class LeftMenusActivity extends ActionBarActivity implements FeedFragment.OnFragmentInteractionListener, UserFragment.OnFragmentInteractionListener,
        SettingsFragment.OnFragmentInteractionListener, CreateFragment.OnFragmentInteractionListener, TimersFragment.OnFragmentInteractionListener,
        CalendarFragment.OnFragmentInteractionListener, DetailFragment.OnFragmentInteractionListener, SingleWorkoutItemDetailFragment.OnFragmentInteractionListener {

    public static final String LEFT_MENU_OPTION = "com.modup.app.LeftMenusActivity";
    public static final String LEFT_MENU_OPTION_1 = "Left Menu Option 1";
    public static final String LEFT_MENU_OPTION_2 = "Left Menu Option 2";

    private ListView mDrawerList;
    private List<DrawerItem> mDrawerItems;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private SingleWorkout currentSingleWorkoutObject;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_left_menus);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_view);

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        prepareNavigationDrawerItems();
        setAdapter();
        //mDrawerList.setAdapter(new DrawerAdapter(this, mDrawerItems));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
                R.string.drawer_open,
                R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            mDrawerLayout.closeDrawer(mDrawerList);
            FragmentManager fragmentManager = getFragmentManager();
            Fragment mFragment = new FeedFragment();
            fragmentManager.beginTransaction().replace(R.id.content_frame, mFragment).commit();

        }
    }

    private void setAdapter() {
        String option = LEFT_MENU_OPTION_1;
        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.containsKey(LEFT_MENU_OPTION)) {
            option = extras.getString(LEFT_MENU_OPTION, LEFT_MENU_OPTION_1);
        }

        boolean isFirstType = true;

        View headerView = null;
        if (option.equals(LEFT_MENU_OPTION_1)) {
            headerView = prepareHeaderView(R.layout.header_navigation_drawer_1,
                    "http://pengaja.com/uiapptemplate/avatars/0.jpg",
                    "dev@csform.com");
        } else if (option.equals(LEFT_MENU_OPTION_2)) {
            headerView = prepareHeaderView(R.layout.header_navigation_drawer_2,
                    "http://pengaja.com/uiapptemplate/avatars/0.jpg",
                    "dev@csform.com");
            isFirstType = false;
        }

        BaseAdapter adapter = new DrawerAdapter(this, mDrawerItems, isFirstType);

        mDrawerList.addHeaderView(headerView);//Add header before adapter (for pre-KitKat)
        mDrawerList.setAdapter(adapter);
    }

    private View prepareHeaderView(int layoutRes, String url, String email) {
        View headerView = getLayoutInflater().inflate(layoutRes, mDrawerList, false);
        ImageView iv = (ImageView) headerView.findViewById(R.id.imageViewProfilePic);
        TextView tv = (TextView) headerView.findViewById(R.id.email);

        ImageUtil.displayRoundImage(iv, url, null);
        tv.setText(ParseUser.getCurrentUser().getEmail());

        return headerView;
    }

    private void prepareNavigationDrawerItems() {
        mDrawerItems = new ArrayList<DrawerItem>();
        mDrawerItems.add(
                new DrawerItem(
                        R.drawable.feed,
                        R.string.drawer_title_feed,
                        DrawerItem.DRAWER_ITEM_TAG_FEED));
        mDrawerItems.add(
                new DrawerItem(
                        R.drawable.user,
                        R.string.drawer_title_user,
                        DrawerItem.DRAWER_ITEM_TAG_USER));
        mDrawerItems.add(
                new DrawerItem(
                        R.drawable.stopwatch,
                        R.string.drawer_title_timers,
                        DrawerItem.DRAWER_ITEM_TAG_TIMERS));
        mDrawerItems.add(
                new DrawerItem(
                        R.drawable.calendar,
                        R.string.drawer_title_calendar,
                        DrawerItem.DRAWER_ITEM_TAG_CALENDAR));


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            selectItem(position/*, mDrawerItems.get(position - 1).getTag()*/);
        }
    }

    //set up fragment switching here
    private void selectItem(int position/*, int drawerTag*/) {
        // minus 1 because we have header that has 0 position
        FragmentManager fragmentManager = getFragmentManager();
        Fragment mFragment;
        if (position < 1) { //because we have header, we skip clicking on it
            return;
        }
        switch (position) {
            case 1:
                mFragment = new FeedFragment();
                fragmentManager.beginTransaction().replace(R.id.content_frame, mFragment).commit();
                break;
            case 2:
                mFragment = new UserFragment();
                fragmentManager.beginTransaction().replace(R.id.content_frame, mFragment).commit();
                break;
            case 3:
                mFragment = new TimersFragment();
                fragmentManager.beginTransaction().replace(R.id.content_frame, mFragment).commit();
                break;
            case 4:
                mFragment = new CalendarFragment();
                fragmentManager.beginTransaction().replace(R.id.content_frame, mFragment).commit();
                break;
        }
/*      String drawerTitle = getString(mDrawerItems.get(position - 1).getTitle());
        Toast.makeText(this, "You selected " + drawerTitle + " at position: " + position, Toast.LENGTH_SHORT).show();*/

        mDrawerList.setItemChecked(position, true);
        setTitle(mDrawerItems.get(position - 1).getTitle());
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getString(titleId));
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

XML布局

<?xml version="1.0"?>
<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent"
              xmlns:android="http://schemas.android.com/apk/res/android">
    <include layout="@layout/toolbar"/>

    <android.support.v4.widget.DrawerLayout android:layout_height="match_parent" android:layout_width="match_parent"
                                            xmlns:android="http://schemas.android.com/apk/res/android"
                                            android:fitsSystemWindows="true"
                                            android:id="@+id/drawer_layout">


        <FrameLayout android:layout_height="match_parent" android:layout_width="match_parent"
                     android:background="@color/material_grey_200"
                     android:id="@+id/content_frame"/>

        <include android:layout_height="match_parent" android:layout_width="@dimen/drawer_width"
                 layout="@layout/list_view" android:layout_gravity="start"/>

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

</LinearLayout>

工具栏的XML布局

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


    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                       xmlns:app="http://schemas.android.com/apk/res-auto"
                                       android:id="@+id/toolbar"
                                       android:layout_width="match_parent"
                                       android:layout_height="56dp"
                                       android:elevation="4dp"
                                       android:background="@color/primary_blue"
                                       app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
                                       app:theme="@style/AppTheme.Title"/>

1 个答案:

答案 0 :(得分:4)

删除android:fitsSystemWindows="true"或在DrawerLayout中的第一个元素内移动工具栏(添加LL,因为DrawerLayout可以处理2个视图)。