ActionBar 在更新到 Android 5.0(棒棒糖)之后的样式设置
ActionBar 在更新到 Android 5.0(棒棒糖)之后的样式设置
昨天我正在使用API19开发自定义的ActionBar,今天我更新了支持库和项目到API21,这给我带来了问题。在我的ActionBar中,有一个我不需要的菜单到箭头按钮,并且ActionBar的样式也发生了变化。
更新之前:
更新之后:
以下是style.xml的内容:
MainActivity中的onCreate方法:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); // mTitle = getTitle(); // 设置抽屉 mNavigationDrawerFragment.setUp( R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); actionBar = getSupportActionBar(); // Toolbar toolbar =(Toolbar) findViewById(R.id.toolbar); // setSupportActionBar(toolbar); LayoutInflater mInflater = LayoutInflater.from(this); LayoutParams layout = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); View mCustomView = mInflater.inflate(R.layout.actionbar, null); actionBar.setCustomView(mCustomView,layout); actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false); actionBar.setDefaultDisplayHomeAsUpEnabled(false); actionBar.setDisplayUseLogoEnabled(false); }
有没有办法使用最新的SDK恢复ActionBar的样式?
更新:我成功地通过删除Drawerfragment中的ActionBarDrawerToggle来删除ActionBar中的箭头。
现在我只面临ActionBar的样式问题。左侧的边距和ActionBar的背景颜色。
问题出现的原因是在Android Lollipop版本后,AppCompat ActionBar自定义视图不占据整个屏幕宽度。解决方法是通过重新设置工具栏的样式。
解决方法已经在这里得到解决:Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width。
我测试了这个解决方法,可以正常工作,但是你需要重新设置工具栏的样式。