将工具栏动态添加到摘要 activity

Add toolbar dynamically to an abstract activity

我有一个基础 activity,我的所有活动都从中延伸。 class 包括工具栏和导航抽屉等基础知识。

我正在寻找一种解决方案,允许我覆盖扩展活动中的方法,以便我可以更改 activity 将使用的工具栏类型。

public abstract class BaseActivity extends AppCompatActivity {

private Toolbar mToolbar;
private FrameLayout mContentFrame;
private ActivityFragment mActivityFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(setLayout());
    initViews();
    initToolbar();
}

protected int setLayout() {
    return R.layout.activity_base;
}

//I want to override this and provide a different toolbar layout!
protected int setToolbarLayout(){
    return R.layout.toolbar;
}

protected void initViews() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mContentFrame = (FrameLayout) findViewById(R.id.contentFrame);
}

private void initToolbar() {
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mToolbar.setElevation(10f);
    }
}

protected void setContentFragment(ActivityFragment fragment) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(getContentFrame().getId(), fragment).commit();
    supportInvalidateOptionsMenu();
    this.mActivityFragment = fragment;
    setTitle(fragment.getTitle(this));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

public Toolbar getToolbar() {
    return mToolbar;
}

public FrameLayout getContentFrame() {
    return mContentFrame;
}

public ActivityFragment getActivityFragment() {
    return mActivityFragment;
}

我试过在 setContentView() 之后扩充工具栏并为其设置操作栏,但它看起来没有图标或小部件!

好的每个人(无论是谁)我想通了。我所要做的就是在调用 setContentView() 之前膨胀我的 contentView。膨胀后,我添加膨胀的工具栏视图!

public abstract class BaseActivity extends AppCompatActivity {

private Toolbar mToolbar;
private FrameLayout mContentFrame;
private ActivityFragment mActivityFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(inflateLayout());
    initViews();
    initToolbar();
}

protected int getLayoutRes(){
    return R.layout.activity_base;
}

protected int getToolbarLayout(){
    return R.layout.toolbar;
}

private View inflateLayout() {
    View contentView = getLayoutInflater().inflate(getLayoutRes(), null, false);
    attachToolbarToLayout(getLayoutInflater(), (ViewGroup) contentView);
    return contentView;
}

private void attachToolbarToLayout(LayoutInflater inflater, ViewGroup parent){
    mToolbar = (Toolbar) inflater.inflate(getToolbarLayout(),parent,false);
    parent.addView(mToolbar,0);
}

protected void initViews() {
    mContentFrame = (FrameLayout) findViewById(R.id.contentFrame);
}

private void initToolbar() {
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mToolbar.setElevation(10f);
    }
}

protected void setContentFragment(ActivityFragment fragment) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(getContentFrame().getId(), fragment).commit();
    supportInvalidateOptionsMenu();
    this.mActivityFragment = fragment;
    setTitle(fragment.getTitle(this));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

public Toolbar getToolbar() {
    return mToolbar;
}

public FrameLayout getContentFrame() {
    return mContentFrame;
}

public ActivityFragment getActivityFragment() {
    return mActivityFragment;
}