【问题标题】:How can I add a color gradient to the toolbar (Android)如何向工具栏添加颜色渐变(Android)
【发布时间】:2018-01-31 15:22:10
【问题描述】:

我想向我的工具栏(应用栏/标题栏)添加颜色渐变。

我已经在名为“side_nav_bar.xml”的可绘制文件夹中创建了一个 xml 渐变文件。

side_nav_bar.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="135"
    android:endColor="#7524f6"
    android:startColor="#2691f6"
    android:type="linear" />

我在互联网上找到了一些建议来创建一个新的自定义工具栏,然后将其设置为背景图像等。但我没有足够的信息。

我们将不胜感激任何帮助。 谢谢。

【问题讨论】:

    标签: android xml android-studio toolbar


    【解决方案1】:

    试试这个

    ActionBar actionbar = getSupportActionBar();
    actionbar.setBackgroundDrawable(getResources().getDrawable(R.drawable.side_bar_nav));
    

    【讨论】:

    • 我应该在 onCreate 下添加这个吗?
    • 在 onClick 下插入此代码后出现 2 个错误。
    • 1: "错误:(51, 50) 错误: 不兼容的类型: android.support.v7.app.ActionBar 无法转换为 android.app.ActionBar"
    • 2: "错误:(52, 18) 错误: 找不到符号方法 setBackground(int)"
    • 你可以用android.support.v7.app.ActionBar代替ActionBar
    猜你喜欢
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多