【问题标题】:why tablayout scroll up?为什么tablayout向上滚动?
【发布时间】:2018-07-19 12:59:43
【问题描述】:

我正在尝试使用 2 tablayout 创建应用程序,我按照一些步骤进行操作,一切正常,但我遇到了 Tablayout 的问题,在我看到 Toolbartablayout 之间的空间很大时,就像这张照片向上滚动 tablyout 空间消失我想删除这个空间

当我向上滚动时:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#ffffff">
<android.support.design.widget.CoordinatorLayout
    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"
        app:layout_scrollFlags="scroll|snap"
        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|snap"
            android:elevation="5dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_below="@id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMaxWidth="0dp"
            app:layout_scrollFlags="snap|enterAlways"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabIndicatorHeight="4dp"
            android:elevation="10dp"/>
    </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"
        android:background="#FFFFFF"
        app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.CoordinatorLayout>

样式.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

【问题讨论】:

  • 您能否也从Style.xml 发布您的基本主题 - 空白区域可能是第二个工具栏。一种是由您的主题创建的,另一种是您自己添加到布局中的。
  • @Sharj &lt;resources&gt; &lt;!-- Base application theme. --&gt; &lt;style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"&gt; &lt;!-- Customize your theme here. --&gt; &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt; &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark&lt;/item&gt; &lt;item name="colorAccent"&gt;@color/colorAccent&lt;/item&gt; &lt;/style&gt; &lt;/resources&gt;
  • 我更新我的帖子
  • 您在 AppBarLayout 中添加 TabLayout 以便它滚动。

标签: android xml android-tablayout


【解决方案1】:

在您的情况下,额外的空间是上面选项卡布局中 xml 中的工具栏,并且您正在使用主题

"Theme.AppCompat.Light.DarkActionBar"

这里会导致两个操作栏,如果你不使用工具栏就删除它

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#ffffff">
<android.support.design.widget.CoordinatorLayout
    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"
        app:layout_scrollFlags="scroll|snap"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_below="@id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMaxWidth="0dp"
            app:layout_scrollFlags="snap|enterAlways"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabIndicatorHeight="4dp"
            android:elevation="10dp"/>
    </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"
        android:background="#FFFFFF"
        app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.CoordinatorLayout>

这将是您移除工具栏后的最终 xml, 否则你想要工具栏然后设置它们没有操作栏

【讨论】:

    【解决方案2】:

    问题:我没有看到您的 Manifest 文件,但看起来您有一个来自您的 Theme 的默认 Toolbar 和一个您自己设置的。要修复它,您可以删除默认工具栏并使用您添加的工具栏。

    要解决此问题,请按以下步骤操作。

    第 1 步:将 NoActionBar 样式添加到您的 Style.xml 中

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    第 2 步:在 Manifest.xml 中将 AppTheme.NoActionBar 设置为您的 Activity

    <activity
        android:name="YourActivity"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    

    现在您的 Activity 将没有系统工具栏。您可以添加自己的Toolbar 自定义它。

    第 3 步:将您的布局 Toolbar 设置为您的 Activity 中的默认 Toolbar

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

    现在你可以使用这个Toolbar

    setTitle("Some Title");
    

    【讨论】:

      【解决方案3】:

      多余的空间是您在appbarlayout中编写的第二个工具栏。第一个工具栏是默认工具栏。

      您可以通过在 style.xml 中编写以下代码来删除作为工具栏的多余空间

          <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
      

      代替

          <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      

      或者

      您可以修改您的 appbarlayout 代码,如下所示。您刚刚从 appbarlayout 中删除了工具栏。

          <android.support.design.widget.AppBarLayout
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     app:layout_scrollFlags="scroll|snap"
                     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
          <android.support.design.widget.TabLayout
                     android:id="@+id/tabs"
                     android:layout_below="@id/toolbar"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     app:tabMaxWidth="0dp"
                     app:layout_scrollFlags="snap|enterAlways"
                     app:tabGravity="fill"
                     app:tabMode="fixed"
                     app:tabIndicatorHeight="4dp"
                     android:elevation="10dp"/> 
         </android.support.design.widget.AppBarLayout>
      

      【讨论】:

        猜你喜欢
        • 2015-11-25
        • 2015-11-17
        • 2012-06-23
        • 2016-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多