Панель инструментов Android вместо панели действий

Я пытаюсь использовать панель инструментов вместо панели действий. В Android 5.0.+ это работает, но в 4.4 строка состояния находится поверх панели инструментов.

Скриншот:

Скриншот

Макет:

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"   
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"     
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:background="@color/actionbar_color"/>

Добавление панели инструментов в OnCreate():

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Полный макет:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/drawer_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"   
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"     
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:background="@color/actionbar_color"/>

        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:textColor="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="48dip"
            android:background="@color/actionbar_color" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/colors"
            android:layout_below="@+id/tabs"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/left_drawer"
        style="@style/drawer_list"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

         <ListView 
             android:id="@+id/mainMenu"
             android:layout_marginTop="10dp"
             android:layout_marginBottom="10dp"
             android:layout_width="match_parent"
             android:divider="@android:color/transparent"
             android:layout_height="wrap_content" />

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

person Mageno    schedule 15.02.2015    source источник


Ответы (6)


Добавьте frameLayout и в его высоте используйте @dimen/statusBarHeight. Затем вы можете создать файл attrs.xml для каждой версии (нормальной, v19 и v21). Внутри каждого attrs.xml поместите <dimen name="statusBarHeight">25dp</dimen> Измените 25dp на 0 в обычном режиме, 25 в v19 и 0 в v21. Вы также можете создать attrs.xml для ландшафтного режима (values-land, values-land-v19 и т. д.).

<FrameLayout
        android:id="@+id/statusBar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/statusBarHeight"
        android:background="?colorPrimaryDark"
        android:translationZ="4dp" />

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

Взгляните на эту ссылку: Github

person JavierSegoviaCordoba    schedule 15.02.2015

Для Китката добавьте:

android:fitsSystemWindows="true"
person Djordje Tankosic    schedule 15.02.2015
comment
Вы хотите добавить android:fitsSystemWindows=true на панель инструментов? - person Mageno; 16.02.2015
comment
В вашем основном макете, где находится панель инструментов. - person Djordje Tankosic; 16.02.2015
comment
К сожалению не помогло. Я добавил полный файл макета в пост - person Mageno; 16.02.2015
comment
У меня была та же проблема, и она должна работать. Вы пытались добавить android:fitsSystemWindows=true в LinearLayout? - person Djordje Tankosic; 16.02.2015

Учебник: http://www.viralandroid.com/2015/08/android-toolbar-example.html

Добавьте в свой макет XML

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#2196F3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.Toolbar>

В Яве

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
person Half Moon    schedule 14.01.2016

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>    
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.azim.innovation.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.azim.innovation.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

Цвета

<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

Стили

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

Styles-v21

<resources>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

Dependency

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
person Azim Ansari    schedule 01.05.2016

Попробуйте использовать тему ThemeOverlay.NoActionBar. Он не добавляет панель действий

person ElectroDEV    schedule 28.06.2017

Вы изменили тему в styles.xml?

<resources>

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

</resources>

Если вы используете панель инструментов, она должна быть NoActionBar в теме.

Источник: Пример панели инструментов Android.

person Kashfa Khan    schedule 05.11.2017