我正在使用自定义操作栏视图,如下面的屏幕截图所示,操作栏中有一个空白的灰色空间.我想删除它.

我做了什么:
RES /值-V11/styles.xml
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
<item name="actionBarStyle">@style/ActionBarStyle</item>
</style>
Run Code Online (Sandbox Code Playgroud)
RES /值/ my_custom_actionbar.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:height">60dp</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
表现
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/AppName"
android:theme="@style/AppBaseTheme" >
<!-- activities... etc -->
</application>
Run Code Online (Sandbox Code Playgroud)
主要活动
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
ActionBar actionbar = getSupportActionBar();
actionbar.setDefaultDisplayHomeAsUpEnabled(false);
actionbar.setDisplayHomeAsUpEnabled(false);
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayUseLogoEnabled(false);
actionbar.setHomeButtonEnabled(false);
// Add the custom layout
View view = LayoutInflater.from(this).inflate(R.layout.actionbar, null, false);
actionbar.setCustomView(view);
}
Run Code Online (Sandbox Code Playgroud)
我发现了一篇最近的帖子,指出最新版本存在问题.我还将ADT和SDK更新到Android 5.
我不知道该怎么办.
编辑(部分解决方案):
不适用于Android <= API 10. …
所以,我刚刚将我的代码库更新为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的整个宽度?
android android-appcompat android-actionbar android-5.0-lollipop
我正在尝试使用自定义视图的简单操作栏,但我得到以下结果:
为了演示,我创建了一个带有黄色背景颜色的简单xml,它应该占据整个宽度.
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/yellow"
android:orientation="vertical" >
<TextView
android:visibility="gone"
android:id="@+id/action_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/action_save"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textColor="@android:color/white"
android:text="@string/save" />
<ImageView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_actionbar_logo" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中使用以下主题:
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light"></style>
Run Code Online (Sandbox Code Playgroud)

这是操作栏初始化代码:
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.actionbar);
Run Code Online (Sandbox Code Playgroud)