我的 activity_main.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
问题是工具栏没有显示。但是每当我用以下元素包围它时,它就会显示:
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:theme="@style/AppTheme.AppBarOverlay">
什么是 AppBarLayout?!
我感谢每一个帮助,
LG男:)
最佳答案
follow the official doc to know more about appbarlayout
AppBarLayout 是一个垂直的 LinearLayout,它实现了 Material 设计应用栏概念的许多功能,即滚动手势。
children 应该通过 setScrollFlags(int) 和相关的布局 xml 属性提供他们想要的滚动行为:app:layout_scrollFlags。
此 View 在很大程度上取决于在 CoordinatorLayout 中用作直接 subview 。如果您在不同的 ViewGroup 中使用 AppBarLayout,它的大部分功能将无法使用。
AppBarLayout 还需要一个单独的滚动兄弟,以便知道何时滚动。绑定(bind)是通过 AppBarLayout.ScrollingViewBehavior 行为类完成的,这意味着您应该将 ScrollView 的行为设置为 AppBarLayout.ScrollingViewBehavior 的一个实例。包含完整类名的字符串资源可用。
<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"
tools:context="com.mgh.jaatrabackoffice.activity.DashboardActivity">
<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_dashboard" />
<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="@drawable/ic_date_range_white_24dp"
android:visibility="visible"
app:backgroundTint="#ffbb33"/>
如果您想要自定义工具栏,请使用此代码而不是工具栏
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="#e91e63"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageButton
android:id="@+id/back_imagebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@android:color/transparent"
android:padding="5dp"
android:src="@drawable/ic_back_button" /> <!--android:tint="#e91e63"-->
<ImageButton
android:id="@+id/usericon_imagebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@android:color/transparent"
android:padding="5dp"
android:src="@drawable/user"
android:visibility="visible"
android:tint="#fff"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@android:color/white"/>
关于java - 为什么不显示工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50492550/