android - Lollipop 之前的设备中未出现工具栏

标签 android navigation-drawer android-appcompat android-toolbar drawerlayout

工具栏未出现在 Lollipop 之前的设备上。我正在使用带工具栏的抽屉布局。相同的工具栏适用于其他 Activity ,但与抽屉布局一起使用时则不然。

带有抽屉布局和工具栏的 Activity

@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>

ma​​in_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>

最佳答案

这是一个常见问题,在 Lollipop 或更高版本上进行测试时变得不太明显。

当使用标准布局(例如 FrameLayoutRelativeLayout 等)时,默认行为是 subview 按照添加或膨胀的顺序绘制。

修复看起来与此类似:

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

然而,自从 Lollipop 以来,android:elevation 属性可以覆盖此行为。由于高程定义了沿 Z 轴的精确位置,具有较高高程值的 View 将绘制在具有较低高程值的 View 之上。

关于android - Lollipop 之前的设备中未出现工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32668480/

相关文章:

Android Studio/com.android.support :appcompat-v7:27. 1.1 红色下划线

java - 由于应用程序兼容,Searchview 不起作用

android - android studio 1.2.1 中已经定义了属性 "titleTextStyle"?

android - 使用微调器从自定义 ListView 中删除了错误的行

java - 缩小操作栏后,抽屉导航切换不起作用

java - 如何交换两个圆

reactjs - 如何删除抽屉导航上的阴影覆盖?

android - Android 中常见的导航菜单抽屉

java - Android 4.4 及更高版本在重启后不提供持久文件路径

android - 后退按钮在第一个 Activity 上的功能