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

有许多关于如何实现自定义标题栏的教程和问题。但是,在我的自定义标题栏我有一个自定义渐变的背景,我想知道如何在我的代码动态设置它。

下面是我的自定义标题栏被调用的地方:

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 文件中,就像它们现在这样。

如果你有更好的设置背景渐变的方法,我对所有的想法都持开放态度。

先谢谢你! !

99533 次浏览

To do this in code, you create a GradientDrawable.
The only chance to set the angle and color is in the constructor. If you want to change the color or angle, just create a new GradientDrawable and set it as the background

    View layout = findViewById(R.id.mainlayout);


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


layout.setBackgroundDrawable(gd);

For this to work, I added an id to your main LinearLayout as follows

<?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>

And to use this as for a custom title bar

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

If accepted answer is not working, you can try to set foreground.

color.setForeground(gradient);

Answer with gradient generation is correct, but it will not work if you just set background to MaterialButton. For MaterialButton you have to nullable default backgroundTint:

app:backgroundTint="@null"

After that solution will work. (link to source: https://github.com/material-components/material-components-android/issues/889#issuecomment-621194246)

if you are using Kotlin then write

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