其他分享
首页 > 其他分享> > Android工具栏被推到顶部状态栏的后面

Android工具栏被推到顶部状态栏的后面

作者:互联网

我最近将手机更新为Android 5.0.2,现在正在开发的应用程序出现了一个工具栏问题.工具栏被向上推,并被最顶部的状态栏部分隐藏.

什么会导致此问题?

这是活动的代码,具有用于处理工具栏的布局

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"/>

解决方法:

删除android:fitsSystemWindows =“ true”或将工具栏移动到DrawerLayout的第一个元素内(添加LL,因为DrawerLayout可以在其​​中使用2个视图).

标签:android-toolbar,android
来源: https://codeday.me/bug/20191028/1953026.html