zoukankan      html  css  js  c++  java
  • 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);
        }
  • 相关阅读:
    MES取所有部门的函数实例
    MES总结:CBF.Common 文件Net下的有类型转换
    LINQ to SQL 系列 如何使用LINQ to SQL插入、修改、删除数据
    c# abstract抽象类与继承类子类的构造函数_base
    C# 之泛型详解
    MVC6与Asp.net5
    MenuStrip菜单递归
    .net 下的MVCPager
    UVALive5461 UVA615 POJ1308 HDU1325 Is It A Tree?
    UVALive5461 UVA615 POJ1308 HDU1325 Is It A Tree?
  • 原文地址:https://www.cnblogs.com/xiaoQLu/p/2757100.html
Copyright © 2011-2022 走看看