【问题标题】:Transparent Gradient Toolbar透明渐变工具栏
【发布时间】:2017-10-18 05:33:06
【问题描述】:

我正在尝试制作透明的工具栏渐变,但没有任何效果。这是我的代码。

activity_main.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"
    tools:context="com.amrat.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

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

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

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

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

gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="270"
        android:endColor="#00ffffff"
        android:startColor="#ffffff"
        android:type="linear" />
</shape>

尝试这样做:

但是得到了这个:

如果有人知道怎么做,请帮助我。

更新

AndroidManifest.xml

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

style.xml

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

    <item name="colorControlNormal">@color/light_grey_1</item>
    <item name="colorControlActivated">@color/orange_1</item>
    <item name="colorControlHighlight">@color/light_grey_2</item>
</style>

【问题讨论】:

  • 为什么不直接在工具栏中设置透明色而不是创建渐变背景
  • 它已经是透明的,但您缺少一些主题行为。发送您的清单和 styles.xml
  • @Bhavnik 因为我不想要完全透明的工具栏。请参阅预期图像。
  • @EmreAktürk 在帖子中添加。

标签: java android toolbar gradient transparent


【解决方案1】:

直接使用@android:color/transparent。关注这个—— 工具栏布局 -

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar
    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/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/background_toolbar_transparent" />

background_toolbar_transparent.xml

<?xml version="1.0" encoding="utf-8"?>

<shape
    xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient
        android:angle="270"
        android:endColor="@android:color/transparent"
        android:startColor="@color/black_alpha"/>
</shape>

在 colors.xml 上添加 coloralpha -

<color name="black_alpha">#66000000</color>

【讨论】:

  • 你不想要任何渐变背景吗?只需要透明度??所以,只需在 toolbar 布局上使用 android:background="@android:color/transparent" 而不是 android:background="@drawable/background_toolbar_transparent"
  • 兄弟,您的代码正在运行,我只是错误地应用了它。谢谢;)
【解决方案2】:

您需要将startend 颜色更改为透明。

<gradient
    android:angle="270"
    android:startColor="@android:color/transparent"
    android:endColor="@android:color/transparent"
    android:type="linear" />
</shape>

【讨论】:

  • 仅将背景渐变应用到AppBarLayout 并保留工具栏背景。
【解决方案3】:
<?xml version="1.0" encoding="utf-8"?>
<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"
   android:background="@android:color/holo_red_dark"
   android:orientation="vertical">

   <!-- Your content comes here -->

   <android.support.v7.widget.Toolbar
       android:id="@+id/layout_toolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="@android:color/transparent"
       android:elevation="6dp"
       tools:targetApi="lollipop"/>
</FrameLayout>

Frame Layout 基本上用作堆栈,最后添加的项目位于顶部。

祝你好运

埃姆雷

【讨论】:

  • 你的意思是我应该删除 AppBarLayout ?
  • 是的,除非有理由保留它,否则用地雷替换全部内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
  • 2012-02-22
  • 2014-01-11
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
相关资源
最近更新 更多