【问题标题】:Change toolbar color, height and views during runtime在运行时更改工具栏颜色、高度和视图
【发布时间】:2015-10-14 20:48:54
【问题描述】:

我有一个带有导航视图和工具栏的活动应用程序。当用户从导航视图中选择选项时,我想更改工具栏高度(从标准 56dp 到“扩展”-> 128dp https://www.google.com/design/spec/layout/structure.html#structure-app-bar),而 Iam 不更改活动只替换片段。那么最好的解决方案是什么?在工具栏类和 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);
}

【问题讨论】:

  • “在工具栏类和 ActionBar 中不是设置 layout_height 的方法”是什么意思? Android 中的每个 View 都有一个与之关联的 LayoutParams,您可以通过它来更改它的高度。

标签: android material-design toolbar navigation-drawer


【解决方案1】:

我通过以下方式解决了它:

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

还有我的dimens.xml:

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

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

【讨论】:

    猜你喜欢
    • 2010-09-20
    • 2020-05-09
    • 2010-12-10
    • 2019-03-11
    • 2021-07-19
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多