开发者

Change Android title bar colour *after* the activity has loaded?

I change the title bar when the version of Android supports using the code posted below. But I don't seem to be able to change the colour of the title bar once the Activity has actually loaded.

See the following example:

activity.getWindow().setFeatureInt(Window.FEATUR开发者_运维问答E_CUSTOM_TITLE, R.layout.page_title);
LinearLayout ll = ((LinearLayout)activity.getWindow().findViewById(R.id.page_title_bg));
ll.setBackgroundResource(R.drawable.page_title_bg_app_online);

The layout is very simplistic, only containing a background layout and a TextView to show the application's title.

I've tried setting the background resource with setBackbroundResrouce but have not been able to get the changed title to appear. I've also tried invalidating the layout after I make the change.


Just a thought: did you try to set background of your text view (not layout)?

Weird... Just tried and it works fine for me:

    boolean isTitleCustomizible;
    try {
        isTitleCustomizible = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    } catch (Exception e) {
        isTitleCustomizible = false;
    }

    super.setContentView(resId);

    if (isTitleCustomizible) {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.title_bar);

        View root = getWindow().findViewById(R.id.toolBarRoot);
        if (root != null) {
            root.setBackgroundResource(android.R.drawable.editbox_background_normal);
        }
    }

The code in onClick:

        private boolean toggle;

        @Override
        public void onClick(View v) {
            View root = findViewById(R.id.toolBarRoot);
            if (root != null) {
                if (toggle)
                    root.setBackgroundResource(0);
                else
                    root.setBackgroundResource(android.R.drawable.editbox_background_normal);
                toggle = !toggle;
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜