android 公共頂部欄

以下代碼寫在baseActivity中,下面這種方式是LinearLayout,你也可以改成RelativeLayout,當時頂部始終在第一個
界面,你可以 在setContentView的後面再加,看我紅色畫線部分,第二個紅色畫線刪除。
/**
     * group : 爺爺(findViewById(android.R.id.content);android.R.id.content是Android內部提供的每一個活動的根佈局都是這個id)
     * parentLinearLayout:父親
     * activityBase:兒子
     * <p>
     * group.addView(parentLinearLayout); 表示將父親的佈局依附到爺爺的佈局上
     * LayoutInflater.from(this).inflate(activityBase, parentLinearLayout, true);//表示將兒子的佈局依附到父親佈局上,true表示同意依附的意思
     * 通過以上兩句顯然爺爺,父親,兒子的佈局的關係是相互聯繫的,都是按照順序鏈接在一起的
     */
//    private int layoutResId;
    private void initContentView(int layoutResID) {
        //得到窗口的根佈局
        ViewGroup group = (ViewGroup) findViewById(android.R.id.content);
        //首先先移除在根佈局上的組件
        group.removeAllViews();
        //創建自定義父佈局
        parentLinearLayout = new LinearLayout(this);
        parentLinearLayout.setOrientation(LinearLayout.VerTICAL);
        //將自定義的父佈局,加載到窗口的根佈局上
        group.addView(parentLinearLayout);
//        this.layoutResId=layoutResID;
        //這句話的意思就是將自定義的子佈局加到parentLinearLayout上,true的意思表示添加上去
        LayoutInflater.from(this).inflate(layoutResID, parentLinearLayout, true);
    }

    /**
     * 這句的意思表示將MainActivity的佈局又加到parentLinearLayout上
     */
    @Override
    public void setContentView(int layoutResID) {
        LayoutInflater.from(this).inflate(layoutResID, parentLinearLayout, true);
        //這句話的意思就是將自定義的子佈局加到parentLinearLayout上,true的意思表示添加上去
//        LayoutInflater.from(this).inflate(layoutResId, parentLinearLayout, true);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        parentLinearLayout.addView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        parentLinearLayout.addView(view, params);
    }
發佈了37 篇原創文章 · 獲贊 19 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章