toolbar not appearing in pre lollipop devices

不问归期 提交于 2020-01-01 12:25:27

问题


Toolbar is not appearing on pre lollipop devices. I'm using drawer layout with toolbar. The same toolbar works on other activities but not when used along with drawer layout.

Activity with drawer layout and toolbar

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


              setContentView(R.layout.activity_home_screen);

                {

                    /*supportChatImageButton = (ImageButton) findViewById(R.id.homeSupportChatImageButton);
                    orderImageButton = (ImageButton) findViewById(R.id.homeOrderImageButton);*/
                    headerTextView = (TextView) findViewById(R.id.homeHeaderTextView);
                    slidingTabLayout = (SlidingTabLayout) findViewById(R.id.homeSlidingTabLayout);
                    viewPager = (ViewPager) findViewById(R.id.homeViewPager);
                    headerBackgroundImageView = (ImageView)findViewById(R.id.homeHeaderBackgroundImageView);

                    mainContentLayout = (FrameLayout) findViewById(R.id.homeScreenMainContentLayout);
                    hamburgerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                    hamburgerLinearLayout = (LinearLayout) findViewById(R.id.hamburgerProfileLayout);
                    hamburgerListView = (ListView) findViewById(R.id.hamburgerListView);
                    hamburgerMenu = (RelativeLayout) findViewById(R.id.hamburgerMenu);
                    GetHamburgerListItems();
                    HamburgerListAdapter hamburgerListAdapter = new HamburgerListAdapter(this, hamburgerListItems);
                    hamburgerListView.setAdapter(hamburgerListAdapter);

                    userProfileImageView = (ImageView) findViewById(R.id.userProfileImageView);
                    userProfileNameTextView = (TextView) findViewById(R.id.userProfileNameTextView);

                    if(StorageManager.read(this, "userProfile", new TypeToken<UserProfile>() {}.getType()) != null) {
                        UserProfile userProfile = (UserProfile) StorageManager.read(this, "userProfile", new TypeToken<UserProfile>() {}.getType());
                        userProfileNameTextView.setText(userProfile.getFullName());
                    }
                    else {
                        userProfileNameTextView.setText("Login / Sign up");
                    }

                    Typeface typeface = Typeface.createFromAsset(this.getAssets(), "fonts/GothamRnd-Medium.otf");
                    slidingTabLayout.setTypeface(typeface);
                    slidingTabLayout.setTextColor("#FFFFFF");
                }

                if(Utilities.IsCitySelected(this)) {
                    selectedCity = Utilities.GetSelectedCity(this);
                }
                else {
                    Intent intent = new Intent(HomeScreenActivity.this, ErrorInternetActivity.class);
                    intent.putExtra("error", "error");
                    startActivity(intent);
                    finish();
                }


                toolbar = (Toolbar) findViewById(R.id.main_toolbar);
                toolbar.setNavigationIcon(R.drawable.ic_action_image_dehaze);
                toolbar.inflateMenu(R.menu.home_screen_menu);
                toolbar.setBackgroundColor(Color.TRANSPARENT);
                setSupportActionBar(toolbar);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
                getSupportActionBar().setDisplayShowTitleEnabled(false);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);

                actionBarDrawerToggle = new ActionBarDrawerToggle(this, hamburgerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
                    @Override
                    public void onDrawerOpened(View drawerView) {
                        super.onDrawerOpened(drawerView);
                        invalidateOptionsMenu();
                        syncState();
                    }

                    @Override
                    public void onDrawerClosed(View drawerView) {
                        super.onDrawerClosed(drawerView);
                        invalidateOptionsMenu();
                        syncState();
                    }
                };

                hamburgerLayout.setDrawerListener(actionBarDrawerToggle);
                hamburgerLayout.setDrawerListener(this);

        hamburgerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    publ

    ic void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                    SelectItemFromDrawer(position);
                }
            });

            hamburgerLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(Utilities.IsUserLoggedIn(HomeScreenActivity.this)) {
                        hamburgerLayout.closeDrawer(hamburgerMenu);
                        isLinkClicked = true;
                        position = 2;
                    }
                    else {
                        hamburgerLayout.closeDrawer(hamburgerMenu);
                        isLinkClicked = true;
                        position = 3;
                    }
}

                }
            });

Activity - XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeScreenActivity">

    <FrameLayout
        android:id="@+id/homeScreenMainContentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.6">

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

                    <ImageView
                        android:id="@+id/homeHeaderBackgroundImageView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:scaleType="fitXY" />

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="42dp"
                            android:layout_alignParentTop="true"
                            android:layout_marginTop="12dp"
                            android:gravity="right">

                            <!--<ImageButton
                                android:id="@+id/homeSupportChatImageButton"
                                android:layout_width="36dp"
                                android:layout_height="36dp"
                                android:layout_marginRight="20dp"
                                android:background="@drawable/ic_help"
                                android:visibility="visible" />

                            <ImageButton
                                android:id="@+id/homeOrderImageButton"
                                android:layout_width="36dp"
                                android:layout_height="36dp"
                                android:layout_marginRight="16dp"
                                android:background="@drawable/ic_menu_orders" />-->

                        </LinearLayout>

                        <com.misc.TextView
                            android:id="@+id/homeHeaderTextView"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:gravity="center"
                            android:text="@string/home_screen_header_text"
                            android:textColor="@color/white"
                            android:textSize="24sp"
                            android:textStyle="bold"
                            app:fontName="@string/gotham_bold_reg" />

                        <com.misc.SlidingTabLayout
                            android:id="@+id/homeSlidingTabLayout"
                            fontPath="fonts/GothamRnd-Medium.otf"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true"
                            android:paddingTop="10dp"></com.misc.SlidingTabLayout>

                    </RelativeLayout>

                </FrameLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.40"
                android:background="#FFFFFF">

                <android.support.v4.view.ViewPager
                    android:id="@+id/homeViewPager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#FFFFFF"></android.support.v4.view.ViewPager>

            </LinearLayout>

        </LinearLayout>

    </FrameLayout>

    <!-- The navigation drawer -->
    <RelativeLayout
        android:id="@+id/hamburgerMenu"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <!-- Profile Box -->

        <LinearLayout
            android:id="@+id/hamburgerProfileLayout"
            android:layout_width="match_parent"
            android:layout_height="144dp"
            android:background="@color/lo_orange"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/userProfileImageView"
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:layout_marginLeft="16dp"
                android:scaleType="fitXY"
                android:src="@drawable/ic_person_blue" />

            <com.misc.TextView
                android:id="@+id/userProfileNameTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="16dp"
                app:fontName="@string/gotham_medium_reg"
                android:textSize="14sp"
                android:text=""
                android:textColor="#FFFFFF"/>

        </LinearLayout>

        <!-- List of Actions (pages) -->
        <ListView
            android:id="@+id/hamburgerListView"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_below="@id/hamburgerProfileLayout"
            android:background="#FFFFFF"
            android:choiceMode="singleChoice" />

    </RelativeLayout>

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

main_toolbar.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/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/lo_orange"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:elevation="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.misc.TextView
            android:id="@+id/toolBarTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fontName="@string/gotham_medium_reg"
            android:maxLines="2"
            android:textSize="16sp"
            android:textColor="#FFFFFF"/>

        <com.misc.TextView
            android:id="@+id/toolBarSubTitleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:fontName="@string/gotham_light_reg"
            android:maxLines="1"
            android:textSize="12sp"
            android:visibility="gone"
            android:textColor="#FFFFFF"/>

    </LinearLayout>

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

style.xml

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:textColorHintInverse">@color/lo_orange</item>
        <!--<item name="colorPrimaryDark">@color/dark_orange</item>-->
        <item name="colorPrimary">@color/lo_orange</item>
        <item name="colorAccent">@color/lo_orange</item>

        <!-- API 14 theme customizations can go here. -->
        <item name="android:windowActionBarOverlay">true</item>

        <!-- Support library compatibility -->
        <item name="windowActionBarOverlay">true</item>

        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>

        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>

    </style>

回答1:


This is a common issue becoming less apparent when testing on Lollipop or later.

When using standard layouts (such as FrameLayout, RelativeLayout etc.) the default behavior is that child views get drawn in order they are added or inflated.

The fix would look similar to this:

<FrameLayout>
     <!-- Content will be drawn first - below the toolbar. -->
     <FrameLayout android:id="@+id/main_content" />
     <!-- Toolbar will be drawn next - above the content. -->
     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" />
</FrameLayout>

However since Lollipop the android:elevation attribute can override this behavior. Since elevation defines precise position along the Z axis views with higher elevation values will be drawn above those with lower elevation values.



来源:https://stackoverflow.com/questions/32668480/toolbar-not-appearing-in-pre-lollipop-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!