【问题标题】:How can i put an ImageView underneath a transparent toolbar?如何将 ImageView 放在透明工具栏下方?
【发布时间】:2016-05-05 16:40:11
【问题描述】:

我正在尝试实现类似this .我想要一个透明的工具栏/状态栏,下面有一个 ImageView。

我无法使工具栏透明,因此我尝试使用 CollapsingToolbarLayout 并移除滚动行为。它有效,但我无法使状态栏透明。

有没有办法使工具栏透明并在其下方放置一个 ImageView 或者有更好的方法来实现它?

编辑:

标准 xml 布局。到目前为止我没有改变任何东西。

<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:fitsSystemWindows="true">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" /> </android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 只是想知道。而不是使用 colorPrimary,改变 android:background="@android:color/transparent" 有帮助吗?
  • 我可以将工具栏的颜色更改为我想要的任何颜色,但我无法使其透明。如果我这样做,它仍然是 colorPrimary(蓝色)。如果我将渐变作为背景,它会出现,但背景中仍然是蓝色。
  • 您有当前输出的屏幕截图吗?

标签: android android-toolbar


【解决方案1】:

试试这个。

toolbar.getBackground().setAlpha(0);

或者更好-

transparent Actionbar with AppCompat-v7 21

【讨论】:

  • 感谢您的回答。不幸的是,我无法解决我的问题。第一个解决方案导致了一个白条而不是工具栏。第二个根本没用。我到底应该把RelativeLayout放在哪里?在 AppBarLayout 内?
  • 粘贴您的 xml 以便人们可以查看
  • 尝试在工具栏中使用 android:background="#10000000" 而不是 colorprimary
  • 嗯...不确定,必须自己测试...直到那时可能将所有内容都包装在 relativelayout 中并检查
  • 不。还是一样。
【解决方案2】:

我创建了一个项目来尝试透明状态栏。

您需要在样式中添加以下内容(适用于 v19 和 v21)

<!-- Make the status bar traslucent -->
<style name="AppTheme" parent="AppTheme.Base">
    <item name="android:windowTranslucentStatus">true</item>
</style>

这是布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    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.support.design.widget.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/sun"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/transparent"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

... the rest of my xml

这是 v19 设备布局的输出。

【讨论】:

  • 它不起作用,但它以某种方式将状态栏的颜色更改为深蓝色。
  • 尝试从您的 xml 中删除“fitSystemWindows=true”。
  • 它现在是白色的,我没有看到任何图标。即使我将活动背景设置为红色。
  • 试试saulmm.github.io/mastering-coordinator 它似乎有你要找的东西。他在工具栏后面的图像示例在这里androcode.es/wp-content/uploads/2015/10/simple_coordinator.gif - 答案中的 translucentStatus 部分仍然适用。
  • 我终于让它工作阅读这个答案:@ 987654324@这可能是因为我误解了fitsSystemWindow的效果。感谢您的帮助!
猜你喜欢
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
相关资源
最近更新 更多