【发布时间】:2014-11-13 04:39:34
【问题描述】:
我的问题涉及使用完全自定义的 ActionBar 视图(因为这是我的客户希望 ActionBar 工作的必要条件)。我已经删除了徽标、标题和其他所有可能的内容。但是,ActionBar 的自定义视图不会一直延伸到整个屏幕。
我尝试了以下方法(作为快速概览):
- 动态删除 ActionBar 的各个方面(下面的 sn-p)
- 完全删除选项菜单
- 在样式/主题中指定以从 ActionBar 中删除所有内容
风格如下:(顺便说一句,我最初保留了原生的 ActionBar,但删除了大部分)
<!-- Customized App Theme -->
<style name="AppTheme2" parent="Theme.AppCompat.Light">
<!-- Let the actioBbar overlay the activity, ie activity is full screen -->
<item name="android:windowActionBarOverlay">true</item>
<!-- Set up the action bar styles -->
<item name="android:actionBarStyle">@style/ActionBarEmpty</item>
<!-- Remove the shadow under the actionBar -->
<item name="android:windowContentOverlay">@null</item>
<!-- Set the drawer icon to show up instead of the "Up" caret icon
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
-->
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/ActionBarEmpty</item>
</style>
<!-- Final, empty Action Bar style | makes space for customized actionBar -->
<style name="ActionBarEmpty" parent="Widget.AppCompat.Base.ActionBar">
<!-- TODO: Add support versions -->
<item name="android:displayOptions">none</item>
<item name="android:background">@color/transparent</item>
<!-- Tested the below, does absolute nothing -->
<item name="android:layout_margin">0dp</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">0dp</item>
</style>
这是我的 Activity 中有关设置 ActionBar 的代码:
// Initialize and set up the ActionBar
mActionBar = getActionBar();
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
// Set the actionBar layout initially to the simple one
mActionBar.setCustomView(mViewActionBarSimple);
这是 ActionBar 屏幕截图中显示的视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="@color/white">
<ImageButton
android:id="@+id/navButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/padding_actionbar_icon"
android:paddingBottom="@dimen/padding_actionbar_icon"
android:paddingRight="@dimen/padding_actionbar_icon"
android:paddingEnd="@dimen/padding_actionbar_icon"
android:layout_marginLeft="@dimen/margin_actionbar_icon_left"
android:layout_marginStart="@dimen/margin_actionbar_icon_left"
android:src="@drawable/ic_drawer_normal"
android:background="?android:attr/selectableItemBackground"
/>
<TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_general"
android:layout_gravity="center"
android:textColor="@color/black"
android:textSize="@dimen/text_normal"
android:text="@string/garbage_fill_short"
/>
</LinearLayout>
除了尝试删除我能想到的所有内容,我还尝试删除选项菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// To remove the options menu - and make the custom actionBar take the full space -
// always return false
return false;
}
我在带有 Android KitKat 的三星 Galaxy S4 上运行它。
感谢您阅读本文。在这一点上,任何帮助将不胜感激。
更新:对于阅读本文的任何人,我不知道它为什么会这样,但在遵循以下答案后我遇到了问题。我用RelativeLayout包围了actionBar布局[将LinearLayout及其内容作为子项],并且宽度/背景自行固定......答案仍然令人惊叹,因为它非常清晰并且绝对删除了actionBar,而在我之前仍在以某种方式使用它。
【问题讨论】:
标签: android android-layout android-actionbar