-1

我想在我的应用程序标题栏上设置一个渐变(从#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);

但日食告诉我:

类型视图中的方法 setBackgroundDrawable(Drawable) 已弃用

我该做什么?

4

2 回答 2

2

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:

This method was deprecated in API level 16. use setBackground(Drawable) instead

Depending on the android version you are targeting you might want to continue using the deprecated one.

于 2013-02-24T12:15:51.170 回答
0

titleBar.setBackgroundDrawable(gd);

更改代码

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    setBackgroundDrawable()
} else {
    setBackground();
}
于 2015-02-04T08:43:36.753 回答