在运行时更改工具栏颜色,高度和视图

时间:2015-10-14 20:48:54

标签: android material-design toolbar android-navigation-drawer

我有一个带导航视图和工具栏的活动应用。当用户从导航视图中选择选项时,我想要更改工具栏高度(从标准56dp到“扩展” - > 128dp https://www.google.com/design/spec/layout/structure.html#structure-app-bar),而Iam不会激活,只能替换fragmet。那么这样做的最佳解决方案是什么?在工具栏类和ActionBar中不是设置layout_height的方法。

第二个问题:我想改变从白色到深色的颜色?我必须实例化新工具栏并使用setSupportActionBar方法设置它,因为我不能在运行时设置工具栏的主题?现在我再次调用setSupportActionBar但是这个好的解决方案?

代码:

@SuppressWarnings("ConstantConditions")
private void initToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar_default);
    setDefaultToolbar();

    mDrawerToggler = new ActionBarDrawerToggle(this, mDrawer, R.string.navigation_view_open, R.string. navigation_view_close);
    mDrawer.setDrawerListener(mDrawerToggler);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerToggler.syncState();

    ImageView searchImageView = (ImageView) findViewById(R.id.toolbar_image_view_search);
    searchImageView.setOnClickListener(this);
}

@SuppressWarnings("ConstantConditions")
public void setDefaultToolbar() {
    mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    mToolbar.setTitleTextColor(Color.argb(0, 255, 255, 255));
    mToolbar.setTitle(R.string.app_name);
    setSupportActionBar(mToolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle(getString(R.string.app_name));
}

@SuppressWarnings("ConstantConditions")
public void extendToolbar() {
    mToolbar.setBackgroundColor(getResources().getColor(R.color.toolbarCollapseBackground));
    mToolbar.setTitleTextColor(Color.argb(0, 0, 0, 0));
    //How extend toolbar from 56dp to 128dp here ? I see that is getHeight() method but setHeight() is not avaible ? Using setLayoutParams to set height 128dp ? How ?
    mToolbar.
    setSupportActionBar(mToolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(false);
}

1 个答案:

答案 0 :(得分:0)

我使用以下方法解决了这个问题:

mToolbar.getLayoutParams().height = (int) (int) getResources().getDimension(R.dimen.layout_height_toolbar_extend);

和我的dimens.xml:

<dimen name="layout_height_toolbar_extend">128dp</dimen>

但这是一个很好的解决方案吗?

相关问题