Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 操作栏背景未被更改_Android_Android Actionbar - Fatal编程技术网

Android 操作栏背景未被更改

Android 操作栏背景未被更改,android,android-actionbar,Android,Android Actionbar,我已尝试使用以下代码更改actionbar的背景。它适用于4.3,但不低于4.3。使用以下代码,将设置空背景,即删除旧背景,但不设置新背景。 请帮帮我 public class TestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub su

我已尝试使用以下代码更改actionbar的背景。它适用于4.3,但不低于4.3。使用以下代码,将设置空背景,即删除旧背景,但不设置新背景。 请帮帮我

    public class TestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testing);
    }

    /**
     * Callback when button is clicked to change background
     * @param v
     */
    public void onStartClicked(View v) {
        int Min = 0;
        int Max = 2;

        //Random number generator between 0 and 2 inclusive
        int pos = Min + (int) (Math.random() * ((Max - Min) + 1));

        if (pos == 0) {
            getActionBar().setBackgroundDrawable(
                    getResources().getDrawable(R.drawable.header));
        } else if (pos == 1) {
            getActionBar().setBackgroundDrawable(
                    getResources().getDrawable(R.drawable.inbox_header));

        } else if (pos == 2) {
            getActionBar().setBackgroundDrawable(
                    getResources().getDrawable(R.drawable.outbox_header));

        }

    }
}

您唯一能做的就是在为actionbar设置背景后调用
InvalidateOptions菜单()

 public void onStartClicked(View v) {
        int Min = 0;
        int Max = 2;

        //Random number generator between 0 and 2 inclusive
        int pos = Min + (int) (Math.random() * ((Max - Min) + 1));

        if (pos == 0) {
            getActionBar().setBackgroundDrawable(
                    getResources().getDrawable(R.drawable.header));
        } else if (pos == 1) {
            getActionBar().setBackgroundDrawable(
                    getResources().getDrawable(R.drawable.inbox_header));

        } else if (pos == 2) {
            getActionBar().setBackgroundDrawable(
                    getResources().getDrawable(R.drawable.outbox_header));

        }
       invalidateOptionsMenu();
    }

最后,我找到了解决办法。它是通过设置背景后显示和隐藏动作栏的标题来实现的

getActionBar().setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.inbox_header)); 
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);

谢谢大家的关注。

试着写一行-Drawable tmp=getResources().getDrawable(R.Drawable.outbox\u头)并调试它。tmp是否为空?您是否使用BitmapDrawable作为背景?它在我的S3(Android 4.1.2)中运行良好。谢谢。。我检查了它,但它不是空的。@gunar不,它只是可绘制的。Arun提供的解决方案更干净。您正在执行两个操作,其中包括使actionbar无效。我已经尝试过使选项菜单无效。它对我不起作用。失效对我不起作用,威瑟尔,虽然这不太管用。感谢这个问题只发生在Jelly bean设备上,即4.1.x。此解决方案有效,谢谢这是一个使用SearchView的修复程序,可以放置在onQueryTextSubmit和onSuggestionClick中。我在4.2上遇到了这个问题;它似乎在4.3和4.4中得到了修复。对我来说“invalidateOptions菜单()”非常有效!