Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appcompat-v7: Custom view not properly aligned in ActionBar

I am trying to use Appcompat Toolbar based actionBar

Here is my toolbar.xml

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingBottom="0dp"
android:background="?attr/colorPrimaryDark">

</android.support.v7.widget.Toolbar>

I am including this in my activity.xml file. And then in my Activity's OnCreate method, I am setting a custom PagerStrip into ActionBar

    ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.pager_strip);
    actionBar.setDisplayShowCustomEnabled(true);
    tabs =  (PagerSlidingTabStrip) actionBar.getCustomView().findViewById(R.id.tabs_strip);
    tabs.setViewPager(mPager);

There is some padding below my PagerStrip in ActionBar. I want to remove this padding. here is a picture showing the issue. enter image description here

This was working fine with ActionBarSherlock

like image 682
Shakti Malik Avatar asked Oct 18 '14 21:10

Shakti Malik


1 Answers

I had a similar problem: I have migrated to the Toolbar pattern:

<?xml version="1.0" encoding="utf-8"?>
<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/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    <my.custom.widget.class
            android:id="@+id/tab_bar"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    </my.custom.widget.class>
</android.support.v7.widget.Toolbar>

but the AppCompat gave me a strange padding around my custom view: enter image description here

My fix:

Add app:contentInsetStart="0dp" and app:contentInsetEnd="0dp" to the Toolbar attributes; and android:minHeight="?attr/actionBarSize" to the custom widget attributes.

Result:
enter image description here

Not sure that the solution follows the Material design guidelines, but hope it will help to someone.

like image 56
Mikalai Daronin Avatar answered Nov 15 '22 09:11

Mikalai Daronin