【问题标题】:Android transparent overlay toolbarAndroid透明覆盖工具栏
【发布时间】:2015-02-16 09:21:00
【问题描述】:

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

原来ActionBar的样式只是:

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

我的ToolBar

<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>

【问题讨论】:

    标签: android android-toolbar


    【解决方案1】:

    你在正确的轨道上。最简单的方法是使用FrameLayout,其中内容ViewGroupmatch_parent,而Toolbar 在此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>
    

    【讨论】:

    • 所以这比我所拥有的更接近,我有一个 DrawerLayout 作为我的第二个孩子而不是 LinearLayout,当我从左侧滑动时导航抽屉就在那里,但是 @987654329 @ 不可见,我假设它位于另一个布局的后面,是否需要指定一个属性才能将其置于前面?
    • 来自 xml 的视图按照显示在最高海拔的最后一个元素进行排序。如果将 元素移到 LinearLayout 之后,它将显示在顶部。我编辑了 Nikola 的回复以反映这一点。
    • 已确认。将工具栏移动到最后一个元素已将其置于主要内容的顶部。我的根布局是RelativeLayout。
    • Toolbar 以上CoordinatorLayout 的情况下工作。
    【解决方案2】:

    经过一些锻炼,我做到了。希望它可以帮助别人。在这里,我没有为 appbarlayout

    设置 elevation
     <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"
        android:background="@drawable/background_gradiant"
        android:fitsSystemWindows="true"
        tools:context=".activity.AddMembersActivity">
    
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            app:elevation="0dp">
    
            <android.support.v7.widget.Toolbar
    
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize" />
    
        </android.support.design.widget.AppBarLayout>
    
    
        <include layout="@layout/content_add_member" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 你是天才!
    【解决方案3】:

    你应该在AppBarLayout而不是Toolbar上设置透明度,如果你想让app:layout_behavior覆盖内容,你应该删除它。

    【讨论】:

    • 谢谢!从活动布局中删除app:layout_behavior="@string/appbar_scrolling_view_behavior" 并在AppBarLayout 中设置android:background="@color/transparent" 解决了这个问题。
    • onCreate()中添加toolbar.background.alpha = 0
    【解决方案4】:

    在 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.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" />
    
        <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"/>
    
        <!-- 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>
    

    【讨论】:

    • 按预期工作,但点击底层片段不再起作用。
    【解决方案5】:

    在styles.xml中:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
    </style>
    
    <style name="ActionBar.SemiTransp" parent="Base.Widget.AppCompat.ActionBar">
        <item name="displayOptions">showHome|showTitle</item>
        <item name="background">@drawable/gray_to_transp</item>
    </style>
    
    <style name="AppTheme.SemiTranspActionBar" parent="@style/AppTheme">
        <item name="actionBarStyle">@style/ActionBar.SemiTransp</item>
        <item name="windowActionBarOverlay">true</item>
    </style>
    

    在清单中:

    <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme.SemiTranspActionBar">
    

    【讨论】:

      【解决方案6】:

      CollapsingToolbar 在支持库 22.2.0(2015 年 5 月)中引入,默认提供叠加效果:

      <android.support.design.widget.CoordinatorLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <android.support.design.widget.CollapsingToolbarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <android.support.design.widget.AppBarLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@color/transparent">
      
                  <android.support.v7.widget.Toolbar
                      android:layout_width="match_parent"
                      android:layout_height="?actionBarSize"/>
      
              </android.support.design.widget.AppBarLayout>
      
          </android.support.design.widget.CollapsingToolbarLayout>
      
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        【解决方案7】:

        也许这对你有帮助,瑞恩。

        toolbar.getBackground().setAlpha(0);
        

        【讨论】:

        • 他明确表示他想要Toolbar背后的内容。更改 alpha 不会使内容落后于 Toolbar。如果他只是更改 alpha,抽屉的Rect 将变为黑/白(取决于主题),但内容仍会在下方。
        • 你是对的!也许他可以编辑他的 XML 并组织元素。但是,在这样做之后,我认为他可以使用我发布的线条来让他的工具栏覆盖布局。
        • 他已经将工具栏背景设置为@android:color/transparent。
        • 对,我没看到那行。他必须编辑他的 XML,组织元素并完成。
        • TBH,我已经尝试了所有用于透明条的 SoF 解决方案。在我的例子中,它是 DrawerLayout > CoordinatorLayout > AppBarLayout > Toolbar。我能够让它透明的唯一方法是使用代码,这最终奏效了(setAlpha)。我也将 AppBarLayout 对象上的 setBackgroundColor() 设置为透明。仍在努力摆脱海拔……坦率地说,这是一场噩梦。使用 appcompat-v7:25.3.1 并在 API 26 上进行测试...感谢 Marco。
        【解决方案8】:

        您可以使用 CoordinatorLayout,如下所示

        <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        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"
        android:fitsSystemWindows="true"
        tools:context="com.epbit.itattooz.MainActivity">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:orientation="vertical">
        
            <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="?android:attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:minHeight="?android:attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark"
                android:titleTextAppearance="@style/ToolbarTitle"/>
        
            <FrameLayout
                android:id="@+id/containerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>
        
        </android.support.design.widget.CoordinatorLayout>
        

        frameLayout 用于存放已使用的片段。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-05
          • 1970-01-01
          • 1970-01-01
          • 2015-11-29
          • 1970-01-01
          相关资源
          最近更新 更多