如何在自定义标题栏上以编程方式设置背景色渐变?

96

有很多教程和stackoverflow上的问题实现了自定义标题栏。然而,在我的自定义标题栏中,我有一个自定义渐变背景,我想知道如何在我的代码中动态设置它。

这里是我的自定义标题栏被调用的地方:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.foo_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar); 

这是我的custom_title_bar:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@layout/custom_title_bar_background_colors">
<ImageView   
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:src="@drawable/title_bar_logo"
              android:gravity="center_horizontal"
              android:paddingTop="0dip"/>

</LinearLayout>

你可以看到,线性布局的背景是由这个属性定义的:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient 
    android:startColor="#616261" 
    android:endColor="#131313"
    android:angle="270"
 />
<corners android:radius="0dp" />
</shape>

我的目标是在代码中动态设置渐变颜色。我不想像现在这样在 XML 文件中硬编码它们。

如果你有更好的方法来设置背景渐变,我非常乐意听取所有建议。

提前感谢你!

4个回答

247
为了在代码中实现此功能,您需要创建一个GradientDrawable对象。
您唯一设置角度和颜色的机会是在构造函数中。
如果您想更改颜色或角度,只需创建一个新的GradientDrawable对象,并将其设置为背景即可。
    View layout = findViewById(R.id.mainlayout);

    GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] {0xFF616261,0xFF131313});
    gd.setCornerRadius(0f);

    layout.setBackgroundDrawable(gd);
为了使这个起作用,我按照以下方式向你的主LinearLayout添加了一个id。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<ImageView   
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:src="@drawable/title_bar_logo"
              android:gravity="center_horizontal"
              android:paddingTop="0dip"/>

</LinearLayout>

并且可以将其用作自定义标题栏

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);
    View title = getWindow().findViewById(R.id.mainlayout);
    title.setBackgroundDrawable(gd);

谢谢你的提示。这个方法可以用于小部件吗?我有一个带有渐变形状的小部件,我想改变这个渐变的颜色。在小部件的情况下,这是可能的吗? - Majjoodi
2
@slund 有没有办法从中心开始生成渐变?而不是使用 'GradientDrawable.Orientation.TOP_BOTTOM'? - Louis Evans
@mark 这是你问题的答案 https://dev59.com/iWct5IYBdhLWcg3wn-xz#11947755 - HinoHara
10
自 API 16 起,setBackgroundDrawable()方法已被弃用,建议改用setBackground()来替代或同时使用。详情请参考https://dev59.com/Ll4d5IYBdhLWcg3wJflm 。 - ban-geoengineering
我们如何为RemoteViews设置它? - Pavan Pyati
显示剩余2条评论

4

如果您正在使用 Kotlin,请编写:

val drawable = GradientDrawable().apply {
        colors = intArrayOf(
            R.color.register_gradient_top,
            R.color.register_gradient_middle,
            R.color.register_gradient_bottom_middle,
            R.color.register_gradient_bottom,
        )
        orientation = GradientDrawable.Orientation.TOP_BOTTOM
        gradientType = GradientDrawable.LINEAR_GRADIENT,
        shape = GradientDrawable.RECTANGLE
    }

Source

https://www.android--code.com/2020/06/android-kotlin-create-gradientdrawable.html


1
如果被接受的答案不起作用,您可以尝试设置foreground
color.setForeground(gradient);

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接