Android自定义actionbar



    

        

        

            
        

        

        

            
        
    

    
    

    


private ImageView mTitlebarBack;
    private TextView mTitlebarTitle;
    private FrameLayout contentView;
    private LinearLayout mTitlebar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        // init app
        ((CompanionApplication) getApplication()).appInit();

        initView();
    }

    private void initView() {
        mTitlebarBack = (ImageView) findViewById(R.id.base_titlebar_back);
        mTitlebarTitle = (TextView) findViewById(R.id.base_titlebar_title);
        contentView = (FrameLayout) findViewById(R.id.base_content);
        mTitlebar = (LinearLayout) findViewById(R.id.base_titlebar);
        mTitlebarBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        hideTitleBar();
    }
    
    @Override
    public void setContentView(int layoutResId) {
      LayoutInflater.from(this).inflate(layoutResId, contentView);
    }

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

    @Override
    public void setContentView(View view, LayoutParams params) {
      contentView.addView(view, params);
    }
    
    public void hideTitleBar(){
        mTitlebar.setVisibility(View.GONE);
    }
    
    public void showTitleBar(){
        mTitlebar.setVisibility(View.VISIBLE);
    }
    
    public void setTitleBarTitle(String title){
        mTitlebarTitle.setText(title);
    }

你可能感兴趣的:(Android自定义actionbar)