2012年3月20日 星期二

方法。透過自訂的view設定標題列。

我們需要的功能是能夠自由操作Activity裡面的標題列,而且我要自定義的標題列喔!

整個內容是這樣的:
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        // this.requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        init();
        showAlertDialogInit();//這方法沒有用,是別的地方用的XD

    }

    private void init() {

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.title_on_line);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.title_off_line);
        setTittle(false);

    }

    private void setTittle(boolean onLine) {

        if (onLine) {
            getWindow().findViewById(R.id.title_off_line).setVisibility(
                    View.GONE);
            getWindow().findViewById(R.id.title_on_line).setVisibility(
                    View.VISIBLE);
        } else {
            getWindow().findViewById(R.id.title_on_line).setVisibility(
                    View.GONE);
            getWindow().findViewById(R.id.title_off_line).setVisibility(
                    View.VISIBLE);
        }
    }

要注意幾點:

  1. this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)要放在setContentView(R.layout.main)前面。然後如果放兩個 requestWindowFeature ()他會報錯,如果有知道原因的人還麻煩可以跟我說一下。謝謝了。
  2. 在初始化時,設置:
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.title_on_line);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.title_off_line);
    把畫面都放到title裡面去。
  3. 用這個方法,switch兩個view:
        private void setTittle(boolean onLine) {
    
            if (onLine) {
                getWindow().findViewById(R.id.title_off_line).setVisibility(
                        View.GONE);
                getWindow().findViewById(R.id.title_on_line).setVisibility(
                        View.VISIBLE);
            } else {
                getWindow().findViewById(R.id.title_on_line).setVisibility(
                        View.GONE);
                getWindow().findViewById(R.id.title_off_line).setVisibility(
                        View.VISIBLE);
            }
        }
    
以上,不囉嗦。喵喵!

沒有留言:

張貼留言

你好,我是小書,如果文章內容有錯誤,或是看到有建議以及任何感想時,歡迎提出分享,我們一起學習一起努力。

追蹤者