【问题标题】:Why my Tab Layout and Toolbar are showing like this?为什么我的标签布局和工具栏会这样显示?
【发布时间】:2016-08-05 08:01:52
【问题描述】:

如图所示。这两个没有正确对齐。我无法理解为什么会这样。

我的工具栏位于 app_bar_main.xml 中的协调器布局中,而我的选项卡布局位于 fragment_main 中。我没有在协调器布局中添加选项卡布局,因为我不想在我的所有视图中显示它,只是在我的片段主文件中。

我在 app_bar_main.xml 中的代码:

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

    <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/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

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

fragment_main.xml 中的代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:id="@+id/tabs"
        android:backgroundTint="#FFFFFF"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    </android.support.design.widget.TabLayout>

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

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager_1">

    </android.support.v4.view.ViewPager>

</FrameLayout>

你能告诉我我做错了什么吗?

Image

【问题讨论】:

  • 在样式中为工具栏设置 elevation 0
  • 海拔为 0 @PiyushGupta。
  • 检查我的代码。 @PiyushGupta
  • @AndroidGeek 请刷新。

标签: android android-fragments android-toolbar android-tablelayout


【解决方案1】:

对于那些无法解决这个问题的人。

我能够通过一种方法解决它。

我在 Main Activity 中添加了上述代码,然后将 tablayout 和 Toolbar 在其他片段中的可见性设置为 GONE,在我希望它启用的片段中设置为 VISIBLE。

将可见性设置为消失:

TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
        tabLayout.setVisibility(View.GONE);
        ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.view_pager_1);
        viewPager.setVisibility(View.GONE);

要将其设置为可见,只需将 GONE 替换为 VISIBLE。

【讨论】:

    【解决方案2】:

    app_bar_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
    
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="0dp">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
        </android.support.design.widget.AppBarLayout>
    
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        </FrameLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    fragment_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white">
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
    
            android:layout_width="match_parent"
            android:layout_height="50dp"
    
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabTextColor="@color/empty_area"
            app:tabSelectedTextColor="@android:color/white"
            app:tabIndicatorColor="@android:color/white"
    
            android:background="@color/colorPrimary" />
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
    
            android:layout_height="match_parent"
            android:layout_below="@+id/tab_layout" />
    
        <View
            android:id="@+id/drop_shadow"
            android:layout_width="match_parent"
    
            android:layout_height="3dp"
            android:layout_below="@+id/tab_layout"
            android:background="@drawable/shadow_bottom" />
    
    </RelativeLayout>
    

    另一种方法,正如您提到的,将选项卡布局放在 app_bar_main.xml 中的 AppBarLayout 中

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多