android - appcompat-v7 : Custom view not properly aligned in ActionBar

标签 android android-activity android-appcompat

我正在尝试使用基于 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 下面有一些填充。我想删除这个填充。 这是一张显示问题的图片。 enter image description here

这与 ActionBarSherlock 配合得很好

最佳答案

我有一个类似的问题:我已经迁移到工具栏模式:

<?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 在我的自定义 View 周围给了我一个奇怪的填充: enter image description here

我的修复:

在Toolbar属性中添加app:contentInsetStart="0dp"app:contentInsetEnd="0dp";和 android:minHeight="?attr/actionBarSize" 到自定义小部件属性。

结果:
enter image description here

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

关于android - appcompat-v7 : Custom view not properly aligned in ActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26444753/

相关文章:

android - 使用自定义适配器时,ListView 不会使用主题设置样式

java - Android 启动时崩溃 : Styling the ActionBar Theme App. 兼容

android - Android 1.6 中 ImageSwitcher 的缩放控件?

java - 为什么我的主要函数在声明时没有在我的 Activity 文件中被选取?

android - 如何在不以 Intent 启动 Activity 的情况下从在 list 中注册的广播接收器调用 Activity 中的方法?

android - 显示 Activity 并在加载其元素后

android - 内部 PreferenceScreen 不使用 PreferenceFragmentCompat 打开

android - 在 Android 中使用 PhotoView 显示最初的完整图像

java - 下载并覆盖功能

android - 分配后在 `ViewDataBinding` 单元测试中获取 Activity `Robolectric` 字段