【问题标题】:How can we remove the divider between the ActionBar and the tabs我们如何删除 ActionBar 和选项卡之间的分隔线
【发布时间】:2016-11-19 12:57:43
【问题描述】:

我正在尝试删除 ActionBar 和选项卡之间的分隔线,但我还没有成功。我已经尝试了下面的代码,我得到了如下图所示的结果

有人可以帮我吗?我怎样才能删除这一行

代码:-

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabPatient"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • appbarlayout 下面的toolbar 中添加您的tablayout
  • 请用一些代码解释一下我是android初学者
  • 查看stackoverflow上关于appbarlayout的帖子

标签: android android-viewpager


【解决方案1】:

你应该这样做:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

在你的 java 代码中:

        toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

您还必须在 build.gradle 文件中插入依赖项,但如果您不知道如何实现,请询问我

【讨论】:

  • 我怎样才能实现依赖?
  • 将 `compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1'` 添加到 build.gradle 文件的依赖项中部分
  • 是的,我正在使用 compile 'com.android.support:appcompat-v7:24.2.1' 并 compile 'com.android.support:design:24.2.1'
  • tabLayout = (TabLayout) view.findViewById(R.id.tabPatient); viewPager = (ViewPager) view.findViewById(R.id.view_pager); tabLayout.setBackgroundResource(R.color.colorPrimary); tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#ffffff")); tabLayout.setTabTextColors(Color.parseColor("#727272"), Color.parseColor("#ffffff"));
  • 在我的 java 代码中,我什么也没做,只是以编程方式添加了颜色
【解决方案2】:

用这个结构替换你的tablayout

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/p_toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/bar_color"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
    android:id="@+id/tabPatient"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabGravity="fill"
    app:tabIndicatorHeight="4dp"
    app:tabMode="fixed"
    style="@style/Tab"
    app:tabTextAppearance="?android:textAppearanceMedium">
    </android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>

一个更好的选择是在CoordinatorLayout 中使用所有这些,然后在其中包含您的主要布局...

【讨论】: