appcompat-v7:自定义视图未在ActionBar中正确对齐

Sha*_*lik 7 android android-appcompat android-activity

我正在尝试使用基于Appcompat工具栏的actionBar

这是我的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>
Run Code Online (Sandbox Code Playgroud)

我将其包含在我的activity.xml文件中.然后在我的Activity的OnCreate方法中,我将自定义的PagerStrip设置为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);
Run Code Online (Sandbox Code Playgroud)

ActionBar中的PagerStrip下面有一些填充.我想删除这个填充.这是一张显示问题的图片. 在此输入图像描述

这与ActionBarSherlock一起工作正常

Mik*_*nin 11

我遇到了类似的问题:我已迁移到工具栏模式:

<?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>
Run Code Online (Sandbox Code Playgroud)

但AppCompat在我的自定义视图周围给了我一个奇怪的填充: 在此输入图像描述

我的修复:

添加app:contentInsetStart="0dp"app:contentInsetEnd="0dp"工具栏属性; 以及android:minHeight="?attr/actionBarSize"自定义小部件属性.

结果:
在此输入图像描述

不确定该解决方案是否遵循材料设计指南,但希望它对某人有所帮助.


小智 -2

在某些情况下,工具栏是操作栏的小部件替代品。如果你想使用工具栏那么你可以尝试:

Toolbar=(Toolbar)findViewbyId(R.id.toolbar);
setSupportActionbar(toolbar);
Run Code Online (Sandbox Code Playgroud)

我在DrawerLayout中使用了Toolbar。我的导航抽屉始终可以位于顶部工具栏。

P/S:在ActionBarActivity中使用工具栏