Android 带有 AppCompat-v7 21 的透明操作栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26452431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:29:43  来源:igfitidea点击:

transparent Actionbar with AppCompat-v7 21

androidandroid-appcompatmaterial-design

提问by ligi

Is there a way to make the ActionBar transparent in Material Design via Appcompat-v7 21? This is not working unfortunately.

有没有办法通过 Appcompat-v7 21 使 Material Design 中的 ActionBar 透明?不幸的是,这不起作用。

  <item name="colorPrimary">@android:color/transparent</item>

Also not the old:

也不是旧的:

  <style name="Widget.Styled.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@android:color/transparent</item>
  </style>

回答by Danial Hussain

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ACTION BAR STYLES -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@drawable/actionbar_background</item>
    <item name="android:windowActionBarOverlay">true</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_background</item>
    <item name="windowActionBarOverlay">true</item>
</style>

回答by sancho21

Actually, it's quite easy and simple.

实际上,这很容易和简单。

  1. Just ensure that you use RelativeLayoutto lay Toolbarand the body.
  2. Put the Toolbarin the last.
  3. Put transparent color for android:backgroundattribute of the Toolbar
  1. 只要确保你RelativeLayout用来躺Toolbar和身体。
  2. 放在Toolbar最后。
  3. android:background属性的属性设置透明颜色Toolbar

This is an example of a working code:

这是一个工作代码的例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:baselineAligned="false"
    android:orientation="vertical">

    <fragment
        android:id="@+id/fragment_editor"
        android:name="ichsan.myapp.HelloFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_editor" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="#10000000"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

</RelativeLayout>

Hope it answers the question.

希望它回答了这个问题。

回答by Dhaval Jivani

Step:

步:

1.put below style into v21\styles.xml

1.将下面的样式放入v21\styles.xml

<style name="TransperantToolbar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:windowActionBarOverlay">true</item>
    <item name="windowActionBarOverlay">true</item>
</style>

2.Your toolbar xml code

2.你的工具栏xml代码

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/TransperantToolbar"/>

Output

输出

回答by kellogs

None of the above did the trick for me (or at least not by themselves) using appcompat v7 23 and a CoordinatorLayout, which is most likely the culprit. If you are going that route, then you probably have a main activity layout that looks like

使用 appcompat v7 23 和 CoordinatorLayout(这很可能是罪魁祸首),以上都没有对我有用(或者至少不是他们自己)。如果你要走那条路,那么你可能有一个主要的活动布局,看起来像

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.farmdog.farmdog.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
                 app:layout_anchor="@+id/appbar"
                 app:layout_anchorGravity="top"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!--android:theme="@style/AppTheme.AppBarOverlay">-->
     ...

Notice the two layout anchor lines above - those did it for me.

注意上面的两条布局锚线——它们是为我做的。