我有一个基本活动,我的所有活动都来自。该课程包括工具栏和导航抽屉等基础知识。
我正在寻找一种解决方案,允许我覆盖扩展活动中的方法,以便我可以更改活动将使用的工具栏类型。
LIMIT
我已尝试在setContentView()之后对工具栏进行充气并将操作栏设置为它,但它似乎没有图标或小工具!
答案 0 :(得分:1)
好的每个人(无论是谁担心)我都明白了。我所要做的就是在调用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;
}