Ste*_*kel 96 android android-appcompat android-actionbar android-5.0-lollipop
所以,我刚刚将我的代码库更新为Lollipop,而且我遇到了Action Bar的问题.我正在使用AppCompat和ActionBarActivity,并为自定义视图充气.似乎自定义视图不再占据屏幕的整个宽度,在左侧留下一条细条
过去的样子
它看起来现在的样子
这是我用来设置Action Bar的代码.有人有主意吗?
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();
}
});
}
Run Code Online (Sandbox Code Playgroud)
编辑
取出自定义视图并更改背景现在占用整个宽度.问题是,我们如何让CustomView占用ActionBar的整个宽度?
Muz*_*ant 110
看起来这是由ActionBar最近appcompat-v7更新中的最新更改引起的.看起来你应该如何处理动作栏有很大的变化.
我遇到了同样的问题,在阅读完ActionBar文档后,特别是下面的引文我找到了解决方案.
从Android L(API级别21)开始,操作栏可以由应用程序布局中的任何工具栏小部件表示.应用程序可能会向Activity发信号通知哪个工具栏应被视为活动的操作栏.使用此功能的活动应使用提供的.NoActionBar主题之一,将windowActionBar属性设置为false或不请求窗口功能.
我看到它的方式,AppCompat主题被改变,一方面似乎打破了一些东西,但另一方面提供了更多的灵活性.我建议遵循以下步骤:
.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>
Run Code Online (Sandbox Code Playgroud)
通常建议您在单独的布局文件中执行此操作并include在活动布局中使用,这样,如果在多个活动中使用,您只需在一个位置自定义工具栏
<include layout="@layout/view_action_bar" />
Run Code Online (Sandbox Code Playgroud)
findViewById和setSupportActionBar在您的活动onCreate中signal to the Activity which Toolbar should be treated as the Activity's action barToolbar actionBar = (Toolbar) findViewById(R.id.actionBar);
setSupportActionBar(actionBar);
Run Code Online (Sandbox Code Playgroud)
onCreateOptionsMenu都将添加到工具栏中,并将其视为活动操作栏.Amr*_*dri 51
而不是像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
Run Code Online (Sandbox Code Playgroud)
您只需添加最后两行代码即可解决问题.
我希望这可以帮助你和其他任何人.
更新:在对它进行一些研究后,我发现这种解决方案在某些情况下不起作用.左侧间隙(HOME或BACK)将被移除,但右侧间隙(MENU)将保持原样.以下是这些情况下的解决方案.
View v = getSupportActionBar().getCustomView();
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
v.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)
将这四行添加到上面的代码中,以便右侧间隙也从支持操作栏中删除.
GDA*_*GDA 31
我想你也可以在风格上做到这一点.试试这个.在kitkat上测试了它
<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>
Run Code Online (Sandbox Code Playgroud)
没有其他答案对我有用,所以我看了一下实际的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>
Run Code Online (Sandbox Code Playgroud)
所以显然我们只需要在我们自己的操作栏样式中覆盖这些属性:
<style name="ActionBar" parent="@style/Base.Widget.AppCompat.ActionBar">
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这对我很有用,希望它也能帮助别人.
| 归档时间: |
|
| 查看次数: |
54695 次 |
| 最近记录: |