简体   繁体   English

在Android应用程序中设置标题栏渐变

[英]Set a title bar gradient in an android application

I want to set a gradient on my application title bar (from #F55B53 to #FFFF00 ), with this following code : 我想在我的应用程序标题栏(从#F55B53#FFFF00 )上设置渐变,使用以下代码:

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0xF55B53,0xFFFF00});
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundDrawable(gd);

But eclipse tell me : 但是蚀告诉我:

The method setBackgroundDrawable(Drawable) from the type view is deprecated 类型视图中的setBackgroundDrawable(Drawable)方法已被弃用

What I should do? 我该做什么?

Most of the time you get a deprecation warning, it comes along with the correct way of doing it. 大多数情况下,您会收到弃用警告,并附带正确的警告方式。 If you go to the View class documentation , you will notice that the setBackgroundDrawable(Drawable background) method says: 如果转到View类文档 ,则会注意到setBackgroundDrawable(Drawable background)方法显示:

This method was deprecated in API level 16. use setBackground(Drawable) instead 此方法在API级别16中已弃用。请改用setBackground(Drawable)

Depending on the android version you are targeting you might want to continue using the deprecated one. 根据您要定位的Android版本,您可能要继续使用已弃用的版本。

titleBar.setBackgroundDrawable(gd); titleBar.setBackgroundDrawable(gd);

change the code 更改密码

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    setBackgroundDrawable()
} else {
    setBackground();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM