【问题标题】:Show ImageView partly behind transparent ActionBar在透明的 ActionBar 后面部分显示 ImageView
【发布时间】:2012-11-03 02:40:00
【问题描述】:

Google Maps 应用程序有一个透明的 ActionBar,通过它可以看到地图。

我可以使用这个设置 ActionBar 的透明度:

<style name="Theme.MyTheme" parent="android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">#64000000</item>
</style>

但是如何在 ActionBar 后面显示我的 ImageView?

【问题讨论】:

  • @Niek:你成功了吗?

标签: android actionbarsherlock


【解决方案1】:

您可以启用ActionBar 的覆盖模式。为此,您必须将主题中的(android:)windowActionBarOverlay 项目设置为true

<style name="MyTheme" parent="Theme.Sherlock">
    ...
    <item name="windowActionBarOverlay">true</item> <!-- for ActionBarSherlock -->
    <item name="android:windowActionBarOverlay">true</item>
</style>

【讨论】:

  • 我试过了,但我只得到一个灰色条(以前是白色的),内容不会出现在条后面。我的内容是这个布局,我错过了什么?谢谢 schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
【解决方案2】:

你也可以在运行时设置:

requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

这将使ActionBar成为一个半透明的浮动条。

与任何requestWindowFeature... 一样,应该在添加内容之前调用它。

setContentView 之后,您可以使用Drawable 设置背景:

getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));

getActionBar 更改为 ActionBarSherlock 的 getSupportActionBar

actionbar_bg.xmlshape 的根元素:

<solid android:color="#64000000" />

虽然我发现 Tomik 的 解决方案很棒,但这对于一些活动的一次性案例而不是一刀切的风格很有用。

【讨论】:

  • 虽然 Tomik 的解决方案正是我正在寻找的,但这当然值得一提!谢谢!
  • 这是正确的,我已经尝试过了,但由于某种原因,这在 2.3 上的基于 sherlock 的自定义操作栏上不起作用。奇怪。
  • 您也可以通过创建一个无窗口覆盖样式扩展您的默认样式并将其分配给您不希望窗口覆盖的活动来形成清单。
  • @MarianoLatorre:感谢您为我指出这一点。 ;-) 话虽这么说,我的建议是当你有一个奇怪的活动或者其中一些并不真正保证创建Style 时。因此,Java 解决方案。不过还是谢谢。 :-)
  • 这是有道理的,就我而言,我需要通过许多不同的活动在 windowoverlay 和 no-windowoverlay 之间快速切换,尽管与其他人分享这个选项会很好。
【解决方案3】:

如果有人需要透明栏,但仅用于某些活动,而在其他活动中使用实心栏,则可能值得创建两种不同的样式并使用清单来控制它:

<style name="MyThemeOverlay" parent="Theme.Sherlock">
    ...
    <item name="windowActionBarOverlay">true</item> <!-- for ActionBarSherlock -->
    <item name="android:windowActionBarOverlay">true</item>
    <!-- any stuff common here, colours, etc -->

    <!-- define the style for native ActionBar for Android 4 and higher -->
    <item name="android:actionBarStyle">@style/myActionbarTransparent</item>
    <!-- define the style for ActionBarSherlock -->
    <item name="actionBarStyle">@style/myActionbarTransparent</item>
</style>

<style name="MyThemeNoOverlay" parent="MyTheme">
    <item name="windowActionBarOverlay">false</item> <!-- for ActionBarSherlock -->
    <item name="android:windowActionBarOverlay">false</item>
    <!-- any stuff specific for no overlay activity action bars -->

    <!-- define the style for native ActionBar for Android 4 and higher -->
    <item name="android:actionBarStyle">@style/myActionbar</item>
    <!-- define the style for ActionBarSherlock -->
    <item name="actionBarStyle">@style/myActionbar</item>
</style>

<style name="myActionbar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">@color/white</item>
</style>
<style name="myActionbarTransparent" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">@color/transparent</item>
</style>

然后在您的AndroidManifest.xml 中,您可以将其中一个用作默认值,将另一个用于某些特定活动,方法如下:

<application
    ...
    android:theme="@style/MyThemeOverlay">
    ...
    <activity
      android:name=".Activity1"       
      />
    <activity
      android:name=".Activity2"    
      android:theme="@style/MyThemeNoOverlay"   
      />
    <activity
      android:name=".Activity3"       
      />
    <activity
      android:name=".Activity4"       
      android:theme="@style/MyThemeNoOverlay"
      />

...

【讨论】:

  • 好一个。 +1 的努力。 :-)
【解决方案4】:

在styles.xml中:

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorPrimary">@color/semiTransparent5</item>
</style>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
    <item name="windowActionBarOverlay">true</item>
</style>

在activity_main.xml中:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_main" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2017-01-18
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多