【问题标题】:Why does not display the Toolbar?为什么不显示工具栏?
【发布时间】:2018-05-23 15:44:32
【问题描述】:

我的 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 M:)

【问题讨论】:

标签: java android layout toolbar android-appbarlayout


【解决方案1】:

follow the official doc to know more about appbarlayout

AppBarLayout 是一个垂直线性布局,它实现了 Material Design 应用栏概念的许多功能,即滚动手势。

儿童应通过 setScrollFlags(int) 和相关的布局 xml 属性提供他们想要的滚动行为:app:layout_scrollFlags。

此视图在很大程度上取决于在 CoordinatorLayout 中用作直接子级。如果您在不同的 ViewGroup 中使用 AppBarLayout,它的大部分功能都将不起作用。

AppBarLayout 还需要一个单独的滚动兄弟,以便知道何时滚动。绑定是通过 AppBarLayout.ScrollingViewBehavior 行为类完成的,这意味着您应该将滚动视图的行为设置为 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"/>

【讨论】:

  • 我怎样才能避免在我的例子中使用这样的 AppBarLayout?为什么工具栏不可见?
  • 您可以设计自定义工具栏,或者只使用appbarlayout中的工具栏
  • 我应该在自定义工具栏中使用哪些属性?
  • 我在之前的回答中给出了
  • 我已经给了它..如果它对你有用,请点击正确答案..谢谢@justMe
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
相关资源
最近更新 更多