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

5
我正在尝试使用基于Appcompat Toolbar的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>

我将这个内容包含在我的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);

我的ActionBar中的PagerStrip下方有一些填充。我想要去掉这个填充。以下是显示问题的图片。这在使用ActionBarSherlock时是正常的。
2个回答

10

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

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

但是AppCompat在我的自定义视图周围提供了奇怪的填充:enter image description here

我的解决方法:

app:contentInsetStart="0dp"app:contentInsetEnd="0dp"添加到工具栏属性中,并将android:minHeight="?attr/actionBarSize"添加到自定义小部件属性中。

结果:
enter image description here

不确定该解决方案是否符合材料设计准则,但希望它能对某些人有所帮助。


我正在尝试使用工具栏而不是操作栏,但是遇到了"InflateException: Binary XML file line #21: Error inflating class fragment"的问题。我也在使用navigationdrawer。有没有办法在navigationdrawer中使用工具栏? - Ahmed Nawaz
我尝试通过XML以与您相同的方式添加TabStrip。但是,我的宽度设置为“wrap_content”。但是,当应用程序加载时,操作栏没有显示我的TabStrip。您能否验证即使使用wrap_content也适用于您? - Shakti Malik
对我有用!将app:contentInsetStart="0dp"和app:contentInsetEnd="0dp"添加到Toolbar属性中。 - Kushal Sharma
@AhmedNawaz,您想要在工具栏下方放置选项卡吗? - Eugene H

-2

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

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

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

附注:在ActionBarActivity中使用工具栏


你好。你能分享一下你的类的完整代码吗?我卡在这里了。我正在使用导航抽屉。 - Ahmed Nawaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接