Android 工具栏未显示

Android Toolbar not showing

目前我正在使用 Android 的新工具栏(在 appcompat 中)。它工作正常,直到我尝试以下方法:toolbar.bringToFront()。当我调用它时,工具栏消失了。我的布局:

<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/colorPrimary"
    app:theme="@style/ToolbarTheme"
    app:popupTheme="@style/ToolbarThemePopup"
    />


<com.asdev.sechat.SlidingTabLayout
    android:id="@+id/sliding_tabs"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    />

现在我需要它在前面的原因是 SlidingTabLayout 有一个覆盖工具栏的阴影,我不想要那个。任何人都知道如何将工具栏带到顶部并仍然可见?谢谢

来自文档:

Change the view's z order in the tree, so it's on top of other sibling views. This ordering change may affect layout, if the parent container uses an order-dependent layout scheme (e.g., LinearLayout). Prior to KITKAT this method should be followed by calls to requestLayout() and invalidate() on the view's parent to force the parent to redraw with the new child ordering.

https://developer.android.com/reference/android/view/View.html#bringToFront()

这会弄乱您的布局是有道理的。您可能需要查看相对布局或找到其他方法来摆脱阴影。或者如果你是 min sdk 21,你可以手动设置工具栏上的高度。

相对布局示例:

<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">


<com.asdev.sechat.SlidingTabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="@color/colorPrimary" />

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/sliding_tabs" />

<android.support.v7.widget.Toolbar
    android:id="@id/toolbar"
    android:layout_width="match_parent"
    android:align_parentTop="true"
    android:layout_height="56dp"
    android:background="@color/colorPrimary"
    app:popupTheme="@style/ToolbarThemePopup"
    app:theme="@style/ToolbarTheme" />


</RelativeLayout>

在工具栏的 java 文件中使用 bringToFront 将解决您的问题。

编辑

这是我的应用中的有效解决方案

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:theme="@style/Toolbar" />

    <com.asdev.sechat.SlidingTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/tab_height"
        android:background="@color/primaryColor"
        android:minHeight="@dimen/tab_height"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>