Android 如何使我的TabLayout标题位于工具栏下?

Android 如何使我的TabLayout标题位于工具栏下?,android,android-fragments,android-toolbar,Android,Android Fragments,Android Toolbar,我的应用程序在TabLayout中有5个标签。我决定在MainActivity中初始化它。每个选项卡都是一个片段,每个选项卡都有自己的工具栏,所以我决定在每个片段中分别初始化工具栏。但问题是我的工具栏现在在TabLaout标题下。我想问一下,怎样才能把它们向下移动,或者我应该用另一种方式来组织它们 主要活动: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="h

我的应用程序在TabLayout中有5个标签。我决定在MainActivity中初始化它。每个选项卡都是一个片段,每个选项卡都有自己的工具栏,所以我决定在每个片段中分别初始化工具栏。但问题是我的工具栏现在在TabLaout标题下。我想问一下,怎样才能把它们向下移动,或者我应该用另一种方式来组织它们

主要活动:

<RelativeLayout 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"
  tools:context=".activity.MainActivity">

  <android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    style="@style/AppTabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

</RelativeLayout>
它看起来如何:


一种方法是将
ViewPager
的高度设置为
math\u parent
,并将
TabLayout
置于
ViewPager
上方,顶部空白处为
80dp
留出空间

<RelativeLayout 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"
    tools:context=".activity.MainActivity">

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        style="@style/AppTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</RelativeLayout>

@NileshRathod是因为在每个选项卡中,工具栏应该是不同的,具有不同的按钮和继续。所以,解决这个问题的唯一方法是,我决定在每个xml片段中构建工具栏。那就是创建空的(!)可见工具栏。但我需要移动的那个还在上面。为什么是空的?!我在片段内部创建工具栏,您可以在每个片段中设计它
  private void initUIComponents() {
    mViewPager = findViewById(R.id.viewpager);
    mTabLayout = findViewById(R.id.tabs);
    mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    mViewPager.setAdapter(new MenuCategoryAdapter(getSupportFragmentManager()));
    mTabLayout.setupWithViewPager(mViewPager);

    for (int i = 0; i < mTabLayout.getTabCount(); i++) {
      mTabLayout.getTabAt(i).setIcon(R.drawable.homeicon);
    }
  }
  private void initUIComponents(LayoutInflater inflater, @Nullable ViewGroup container) {
    mRootView = inflater.inflate(R.layout.fragment_home, container, false);

    mToolbarHome = mRootView.findViewById(R.id.toolbar_home);
    mBtnPause = mRootView.findViewById(R.id.btn_pause);

    if (mToolbarHome != null) {
      ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbarHome);
    }

    mBtnPause.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        pauseWiFi(mToolbarHome, mBtnPause);
      }
    });
  }
<RelativeLayout 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"
    tools:context=".activity.MainActivity">

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        style="@style/AppTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</RelativeLayout>
<RelativeLayout 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"
    tools:context=".activity.MainActivity">

    <View
        android:id="@+id/transparent_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="90"
        android:background="#20000000"
        android:visibility="gone" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_home"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="Your Wi-Fi is online">

        <Button
            android:id="@+id/btn_pause"
            android:layout_width="90dp"
            android:layout_height="36dp"
            android:layout_gravity="end"
            android:layout_margin="17dp"
            android:background="@color/white"
            android:text="@string/pause"
            android:textColor="@color/midPurple"
            android:textSize="14sp" />

    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_home"
        android:orientation="vertical">

        <android.support.design.widget.TabLayout
            android:id="@+id/fake_tabs"
            style="@style/AppTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        <!-- Your other views -->
    </LinearLayout>
</RelativeLayout>