Shakti Malik
Shakti Malik

Reputation: 2407

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

Upvotes: 5

Views: 4384

Answers (2)

ductruong
ductruong

Reputation: 1

Toolbar is widget replacement for Actionbar in some cases. If you want to use Toolbar then you can try:

Toolbar=(Toolbar)findViewbyId(R.id.toolbar);
setSupportActionbar(toolbar);

I used Toolbar in DrawerLayout. My navigation drawer can always Top Toolbar.

P/S: use toolbar in ActionBarActivity

Upvotes: -2

Mikalai Daronin
Mikalai Daronin

Reputation: 8716

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.

Upvotes: 10

Related Questions