【问题标题】:Removing Extra Space in Custom ActionBar删除自定义操作栏中的额外空间
【发布时间】: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


    【解决方案1】:

    请在下面尝试通过创建基本活动来删除操作栏内容,以便您可以在所有应用程序中引用并从布局中扩展自定义操作栏

        @SuppressLint("InlinedApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
                getSupportActionBar().hide();
                ActionBar actionBar = getSupportActionBar();
                actionBar.setDisplayHomeAsUpEnabled(false);
                actionBar.setDisplayShowCustomEnabled(true);
                actionBar.setDisplayShowTitleEnabled(false);
                actionBar.setDisplayShowHomeEnabled(false);
                actionBar.setHomeButtonEnabled(false);
    
                actionBar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
                View homeIcon = findViewById(android.R.id.home);
                // Hides the View (and so the icon)
                if (homeIcon != null)
                    ((View) homeIcon.getParent()).setVisibility(View.GONE);
    
                overridePendingTransition(0, 0);
        }
    

    添加自定义视图

    @SuppressLint("InlinedApi")
    @Override
    public void setContentView(int layoutResID) 
    {
        super.setContentView(R.layout.activity_base);
        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);
        viewGroup.removeAllViews();
        viewGroup.addView(getLayoutInflater().inflate(layoutResID, null));
        // you can find action_bar layouts view & add listner
    }
    

    并在activity_base.xml中自定义包含自定义操作栏的XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:orientation="vertical" >
    
        <include layout="@layout/action_bar" />
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right" />
    
    </LinearLayout>
    

    你想在哪里使用然后扩展这个活动。

    【讨论】:

    • 谢谢!尽管我必须进行修改,但这非常有效。对于其他人,我不知道为什么“
    • 更正:“