0

我正在尝试创建一个用户主题的应用程序。我有可点击的线性布局,当布局添加到父视图时,我需要以编程方式设置选择器。但是,当我设置 v.SetBackground() 时,以下代码会引发 NoSuchMetod 异常。

我哪里错了?

           LinearLayout d = (LinearLayout) inflater.inflate(R.layout.adapter_psudo_launcheritem, null);             

            ColorDrawable clrBase = new ColorDrawable();
            clrBase.setColor(gCurrentTheme.Colours.Backgrounds.MenuItem);
            ColorDrawable clrSelect = new ColorDrawable();
            clrSelect.setColor(gCurrentTheme.Colours.Backgrounds.MenuItem_Select);
            ColorDrawable clrPress = new ColorDrawable();
            clrPress.setColor(gCurrentTheme.Colours.Backgrounds.MenuItem_Press);

            StateListDrawable states = new StateListDrawable();
            states.addState(new int[] {android.R.attr.state_enabled}, clrBase);
            states.addState(new int[] {android.R.attr.state_selected}, clrSelect);
            states.addState(new int[] {android.R.attr.state_pressed}, clrPress);

       //This line causes a NoSuchMethod exception!
            v.setBackground(states);
4

1 回答 1

0

View.setBackgroud已被引入api level 16并替换B setBackgroundDrawable(它已被弃用API 16)如果您运行上述代码的移动设备的API级别低于16,您必须使用setBackgroundDrawable

于 2013-06-08T07:55:49.980 回答