Removing Extra Space in Custom ActionBar

2019-05-11 02:32发布

My issue concerns using fully custom ActionBar views (as it's necessary for how my customer wants the ActionBar to work). I've removed the logo, title, and everything else possible. However, the custom view for the ActionBar will not extend all the way across the screen.

I've tried the following (as a quick overview):

  • Dynamically removing aspects of the ActionBar (snippet below)
  • Removing the Options Menu in its entirety
  • Specifying in the Style/Theme to remove everything from the ActionBar

Here is a screenshot of my issue: Situation Screnshot http://imagizer.imageshack.us/a/img540/1289/efkvj9.png

Here is the style: (as a side note, I originally kept the native ActionBar but removed most of it)

<!-- 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>

Here is the code in my Activity that concerns setting up the 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);

Here is the view shown in the screenshot for the 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>

In addition to trying to remove everything I could think of, I also tried to remove the options menu:

@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;
}

I'm running this on a Samsung Galaxy S4 with Android KitKat.

Thank you for reading this. Any help would be appreciated at this point.

Update: For anyone reading this, I don't know why it worked like this, but I came upon the problem after following the below answer. I surrounded the actionBar layout with a RelativeLayout [with the LinearLayout and its contents as the children], and the width/background fixed itself... The answer is still amazing as its very clear cut and absolutely removes the actionBar, while before I was still using it in some way.

1条回答
老娘就宠你
2楼-- · 2019-05-11 03:28

Please try below for removing action bar content by creating base activity so you can refer in all your app & inflate custom action bar from layout

    @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);
    }

add custom view

@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
}

and customise XML containing custom action bar in activity_base.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>

Where you want to use then extend this activity.

查看更多
登录 后发表回答