Android – actionbar background not changed

I have tried to use the following code to change the background of the action bar. It is applicable to 4.3 but not lower than 4.3. Using the following code, i.e. setting null background. The old background is deleted but the new background is not set. Please help me

    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.in@R_871_2419@_header));

        } else if (pos == 2) {
            getActionBar().setBackgroundDrawable(
                    getResources().getDrawable(R.drawable.out@R_871_2419@_header));

        }

    }
}

resolvent:

Finally, I found a solution. After setting the background, I realized it by displaying and hiding the title of the action bar

getActionBar().setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.in@R_871_2419@_header)); 
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);

Thank you for your attention

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>