为特定视图应用新主题,而无需重新创建活动

问题描述 投票:0回答:1

我的应用程序一次显示6个片段,因此重新创建活动不是一个好选择,因为它会使屏幕闪烁并面临一些意外问题,重新保存并恢复活动和片段的状态。

我也不想遍历视图层次结构并更改每个视图,因为有许多属性需要更改。

我只需要为NavigationView应用一个新的主题(样式),它隐藏在屏幕旁边并将显示在后面。特别是,我想更改这些属性:

itemBackgrounditemIconTintitemTextColor

android android-theme
1个回答
0
投票

感谢Mike M

我用他的方式解决了这个问题:

public void updateNavigationViewLayout(){
    drawerLayout.removeView(navigationView);
    LayoutInflater inflater = LayoutInflater.from(new ContextThemeWrapper(this, R.style.newtheme));
    drawerLayout = (DrawerLayout) inflater.inflate(R.layout.navigation_drawer, drawerLayout);
    navigationView = drawerLayout.findViewById(R.id.nav_view);
}

请记住将navigationView引用到新的。否则,在第二次调用此方法时,将不会删除新的NagivationView,您将添加一个额外的NavigationView导致此错误:

Child drawer has absolute gravity LEFT but this DrawerLayout already has a drawer view along that edge
© www.soinside.com 2019 - 2024. All rights reserved.