如何以编程方式设置自定义标题栏上的背景颜色渐变? [英] How do I programmatically set the background color gradient on a Custom Title Bar?

查看:29
本文介绍了如何以编程方式设置自定义标题栏上的背景颜色渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There are many tutorials out there and questions on SO that implement custom title bars. However, in my custom title bar I have a custom gradient for the background and I would like to know how to set it dynamically in my code.

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

Here is where my custom title bar gets called:

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>

如你所见,线性布局上的背景是由这个家伙定义的:

As you can see, the background on the linear layout is defined by this guy:

<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 文件中对它们进行硬编码.

What I would like to do is set those gradient colors dynamically in my code. I do not want to hard code them in my XML file like they currently are.

如果您有更好的方法设置背景渐变,我愿意接受所有想法.

I am open to all ideas if you have a better way of setting a background gradient.

提前谢谢你!!

推荐答案

要在代码中执行此操作,您需要创建一个 GradientDrawable.
设置角度和颜色的唯一机会是在构造函数中.如果你想改变颜色或角度,只需创建一个新的 GradientDrawable 并将其设置为背景

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);

为此,我向您的主要 LinearLayout 添加了一个 id,如下所示

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);

这篇关于如何以编程方式设置自定义标题栏上的背景颜色渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆