android ListActivity自定义标题栏

android 自定义标题栏的步骤是

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        // 这里要主要requestWindowFeature和setContentView先后顺序哦
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
    }

但是这个在ListActivity中好像无效,因为ListActivity没有调用setContentView方法,网上的一些解决方法都是在onCreate中调用setContentView,也就是自己实现layout,

有没有更简单的方法呢,使用上面的那样,

其实是有的,在ListActivity中有一个方法很容易被忽略,ensureList(),这是唯一一个设置了layout的地方,但是不是在oncreate中设置的,listActivity没有实现onCreate方法,

继续找,发现在onRestoreInstanceState(), setListAdapter()和getListView()中调用了,也就是说在这3个方法中都有可能会执行setContentView方法,

知道这个就好办了,

解决方法一:在oncreate方法中调用getListView()代替掉上面的setContentView(R.layout.main)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //代替掉setContentView
        getListView();
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
    }

解决方法二:找到你的activity调用setListAdapter的地方,在后面加上面最后一句

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        /**
         * 你自己的代码
         */
        setListAdapter(adapter);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
    }
posted @ 2012-11-06 15:39  muzhi121  阅读(2827)  评论(0编辑  收藏  举报