Android棒棒糖,AppCompat的ActionBar自定义视图不占用整个屏幕宽度
所以,我刚刚更新了我的代码库棒棒糖,我遇到的问题与操作栏。 我正在使用AppCompat和ActionBarActivity,并膨胀自定义视图。 看起来自定义视图不再占用整个屏幕的宽度,在左边留下一条细条
它曾经看的方式
它看起来现在
这是我用来设置操作栏的代码。 任何人有任何想法?
final ActionBar actionBar = getSupportActionBar(); if(actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(false); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayShowCustomEnabled(true); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setCustomView(R.layout.action_bar_content_search_custom_view); actionBar.setBackgroundDrawable(null); // actionBar.setStackedBackgroundDrawable(null); TextView title = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title); title.setText(R.string.youtube); ImageView back = (ImageView) actionBar.getCustomView().findViewById(R.id.action_bar_back); back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); }
编辑
现在取出自定义视图和改变背景占据了整个宽度。 所以问题是,我们如何使CustomView占据ActionBar的整个宽度?
看起来这是由最近的appcompat-v7更新中对ActionBar的最新更改引起的。 看来你应该如何处理动作条有重大的改变。
我遇到了同样的问题,并阅读了ActionBar文档 ,特别是下面的引用我find了一个解决scheme。
从Android L(API级别21)开始,操作栏可以由应用程序布局中的任何工具栏小部件表示。 应用程序可能会向Activity发出信号,哪个工具栏应被视为Activity的操作栏。 使用此function的活动应使用提供的.NoActionBar主题之一,将windowActionBar属性设置为false或以其他方式不请求窗口function。
我看到的方式, AppCompat主题被改变了,一方面似乎打破了一些东西,另一方面提供了更多的灵活性。 我build议遵循以下步骤:
- 在您的活动中使用
.NoActionBar风格,如上面的报价中所述 - 添加一个
android.support.v7.widget.Toolbar到您的活动布局 - 设置
app:contentInsetStart="0dp"属性。 这是你在问题中描述的边缘的主要问题
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/actionBar" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentInsetEnd="0dp" app:contentInsetStart="0dp" > </android.support.v7.widget.Toolbar>
通常build议您在单独的布局文件中执行此操作,并在活动布局中使用include ,以便在多个活动中使用时只需在一个位置自定义工具栏
<include layout="@layout/view_action_bar" />
- 在您的Activity
onCreate使用findViewById和setSupportActionBarsignal to the Activity which Toolbar should be treated as the Activity's action bar发送signal to the Activity which Toolbar should be treated as the Activity's action bar
Toolbar actionBar = (Toolbar) findViewById(R.id.actionBar); setSupportActionBar(actionBar);
- 一旦你这样做,添加在
onCreateOptionsMenu所有动作将被添加到工具栏,并将被视为活动操作栏。 - 进一步根据需要自定义工具栏(添加子视图等)
而不是像Muzikant所说的那样做很多工作,就像这个答案一样
getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); LayoutInflater mInflater = LayoutInflater.from(this); View mCustomView = mInflater.inflate(R.layout.action_bar_home, null); getSupportActionBar().setCustomView(mCustomView); getSupportActionBar().setDisplayShowCustomEnabled(true); Toolbar parent =(Toolbar) mCustomView.getParent();//first get parent toolbar of current action bar parent.setContentInsetsAbsolute(0,0);// set padding programmatically to 0dp
你只需要添加最后两行代码来解决你的问题。
我希望这可以帮助你和其他人。
更新:在做了一些研究后,我发现这种解决scheme在某些情况下不起作用。 左侧间隙(HOME或BACK)将被删除,但右侧间隙(MENU)将保持原样。 以下是这些情况下的解决scheme。
View v = getSupportActionBar().getCustomView(); LayoutParams lp = v.getLayoutParams(); lp.width = LayoutParams.MATCH_PARENT; v.setLayoutParams(lp);
将这四行添加到上面的代码中,以便右侧的间隙也从支持操作栏中删除。
我想你也可以在风格上做到这一点。 尝试这个。 在kitkat上testing它
<style name="AppTheme" parent="Theme.AppCompat"> <item name="toolbarStyle">@style/AppThemeToolbar</item> </style> <style name="AppThemeToolbar" parent="Widget.AppCompat.Toolbar" > <item name="contentInsetStart">0dp</item> </style>
没有其他答案为我工作,所以我看看在这里find实际的AppCompat v7风格。
如果你看看Base.Widget.AppCompat.ActionBar风格,它有:
<item name="contentInsetStart">@dimen/abc_action_bar_content_inset_material</item> <item name="contentInsetEnd">@dimen/abc_action_bar_content_inset_material</item>
所以显然我们只需要在我们自己的操作栏样式中覆盖这些属性:
<style name="ActionBar" parent="@style/Base.Widget.AppCompat.ActionBar"> <item name="contentInsetStart">0dp</item> <item name="contentInsetEnd">0dp</item> </style>
这对我很好,希望它也能帮助别人。
我今天刚刚遇到这个问题,然后我发现我的项目里面有res/values-v21/styles.xml ,这是由Android Studio自动生成的,这就是原因。
为什么?
因为我的res/values-v21/styles.xml的内容是:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="BaseAppTheme" parent="android:Theme.Material.Light"> </style> </resources>
而我的res/values/styles.xml包含如下内容:
<style name="BaseAppTheme" parent="android:Theme.Holo.Light"> <!-- Customize your theme here. --> <item name="android:windowContentOverlay">@null</item> <item name="android:soundEffectsEnabled">false</item> </style>
然后,当我在Lollipop上运行我的应用程序时,使用了res/values-v21/styles.xml ,这导致了OP所具有的确切问题。 所以我来简单的修复只是删除:
<style name="BaseAppTheme" parent="android:Theme.Material.Light"> </style>
在res/values-v21/styles.xml
在对着显示器敲打我的头之后,这对我有效
Toolbar toolbar = (Toolbar) actionBar.getCustomView().getParent(); toolbar.setContentInsetStartWithNavigation(0); toolbar.setContentInsetEndWithActions(0); toolbar.setContentInsetsAbsolute(0, 0); toolbar.setPadding(0, 0, 0, 0);
getCustomView()。getParent()是什么窍门