Android透明覆盖工具栏

标签 android android-toolbar

我创建了一个 ToolBar 并将其设置为我的 ActionBar 并且我让它透明,我的问题是我希望它覆盖其余内容,现在它就像一个普通的 ActionBar 我的 LinearLayout 在它下面“停止”。如何让我的 ToolBar 覆盖我的布局并让该布局填满整个屏幕?

ActionBar的样式只是:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowActionBarOverlay">true</item>
</style>

我的工具栏:

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

和我的 ToolBar 声明:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setBackgroundResource(Color.TRANSPARENT);
setSupportActionBar(toolbar);

我觉得我错过了一些非常简单的东西,但搜索后我找不到我正在寻找的解决方案。任何帮助,将不胜感激!

编辑: 这是我的完整 XML 主布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Main layout -->
    <FrameLayout
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Nav drawer -->
    <fragment
        android:id="@+id/fragment_drawer"
        android:name="rsay.android.scrollbanner.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left|start" />
</android.support.v4.widget.DrawerLayout>

最佳答案

你在正确的轨道上。最简单的方法是使用 FrameLayout 内容 ViewGroupmatch_parentToolbar 位于顶部这个ViewGroup

<FrameLayout 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"
    tools:context=".MainActivity">


    <!-- This can be any child. For sample purposes I assume this layout contains fragments -->
    <LinearLayout  
        android:background="?attr/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container"
        android:orientation="horizontal"/>


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

</FrameLayout>

关于Android透明覆盖工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27532167/

相关文章:

android - onStop() 和 onDestroy() 可能不会被调用。他们有什么好处?

java - 在 Android Studio 中实现 ArrayList 的最佳方式

java - 以数组形式返回时解析 JSON 响应

android - 查看覆盖操作工具栏?

Android Toolbar 在 CollapsingToolbarLayout 中重叠 TabLayout

android - 是否可以更改 Android SearchView 上的文本颜色?

安卓单元测试 : how to mock a beacon device

android - 动态添加或修改 Sony Smartwatch 2 中的注册信息

android - 如何在工具栏的垂直中心实现后退箭头(向上按钮)

android.support.v4.widget.SwipeRefreshLayout 在 android.support.design.widget.AppBarLayout 内部时不滑动