Android工具栏,而不是操作栏 [英] Android Toolbar instead of action bar
问题描述
我正在尝试使用工具栏而不是ActionBar.在Android 5.0.+中可以使用,但在4.4中,状态栏位于工具栏的顶部.
I'm trying to use Toolbar instead of ActionBar. In Android 5.0.+ it works but in 4.4 the status bar is placed on top of the Toolbar.
屏幕截图:
布局:
<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()中添加工具栏:
Adding Toolbar in 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>
推荐答案
添加一个frameLayout并在其高度中使用@dimen/statusBarHeight
.然后,您可以为每个版本(普通版,v19和v21)创建一个attrs.xml文件.在每个attrs.xml里面放<dimen name="statusBarHeight">25dp</dimen>
在正常情况下将25dp更改为0,在v19中将其更改为25,在v21中将其更改为0.您也可以为横向模式(values-land,values-land-v19等)创建attrs.xml.
Add a frameLayout and in its height use @dimen/statusBarHeight
. Then you can create an attrs.xml file for each version (normal, v19 and v21). Inside of each attrs.xml put <dimen name="statusBarHeight">25dp</dimen>
Change 25dp to 0 in normal, 25 in v19 and 0 in v21. You can create attrs.xml for landscape mode too (values-land, values-land-v19, etc).
<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
这篇关于Android工具栏,而不是操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!