【问题标题】:How to include a TabLayout in a Toolbar in Fragments如何在 Fragments 的工具栏中包含 TabLayout
【发布时间】:2017-04-26 11:52:38
【问题描述】:

我试图在fragment 中使用ViewPagerTabLayout 实现选项卡式视图, 我设法做到了,但选项卡不包含在位于屏幕顶部的工具栏中。 我已经包含了我所拥有的屏幕截图。 有人可以告诉我如何将标签移动到工具栏而且标签不会改变 当它们被触摸时,它们只会在向左或向右滑动时改变。

我尝试在 tabslayout.xml 文件中的android.support.design.widget.AppBarLayout 中添加android.support.design.widget.TabLayout 它在其上添加了蓝色背景,并且触摸有效,但仍不包含在工具栏中。

这里是代码。

1.Tabs.java

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import java.util.ArrayList;
import java.util.List;


public class Tabs extends Fragment {

    public Tabs() {
        // Required empty public constructor
    }

    private TabLayout tabLayout;
    private ViewPager viewPager;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View mekeleLayout = inflater.inflate(R.layout.tabs_layout,container,false);



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

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

        return mekeleLayout;
    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
        adapter.addFrag(new OneFragment(), "RESTAURANTS");
        adapter.addFrag(new TwoFragment(), "CAFES");
        adapter.addFrag(new ThreeFragment(), "HOTELS");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

}

2.tabslayout.xml

<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.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            />



    <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.support.design.widget.CoordinatorLayout>

3.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

4.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.surafel.information.MainActivity">

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

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

    <include layout="@layout/content_main" />


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

【问题讨论】:

    标签: android android-layout android-viewpager android-tablayout


    【解决方案1】:

    这对我有用。

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        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="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:background="@color/colorPrimary"
            android:layout_weight="1">
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="top"
                app:tabIndicatorColor="@color/colorAccent"
                app:tabMaxWidth="0dp"
                app:tabGravity="fill"
                app:tabMode="scrollable"
                android:background="@color/colorPrimary" />
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager
        android:id="@+id/tabanim_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案2】:

      您应该为没有操作栏的活动使用主题。

      【讨论】:

      • 对不起,我是android新手,你能详细描述一下你的答案吗?谢谢
      • 在清单的主要活动的活动标签中添加此代码“
      • 我没有使用活动我正在使用片段
      【解决方案3】:

      试试下面一个

      <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="fill_parent"
      >
      
      <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">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
      
              <android.support.design.widget.AppBarLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
      
                  <android.support.v7.widget.Toolbar 
                          android:id="@+id/app_bar"
                          android:layout_width="match_parent"
                          android:layout_height="?attr/actionBarSize"
                          android:minHeight="?android:attr/actionBarSize"
                          app:layout_scrollFlags="scroll|enterAlways"
                          app:theme="@style/appBar">
      
                  </android.support.v7.widget.Toolbar>
      
                  <android.support.design.widget.TabLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      app:layout_scrollFlags="scroll|enterAlways"
                      app:tabBackground="@color/app_bar_color"
                      app:tabGravity="center"
                      app:tabMode="scrollable" />
              </android.support.design.widget.AppBarLayout>
      
              <android.support.v4.view.ViewPager
                  android:id="@+id/viewpager"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:theme="@style/viewPagerTheme"
                  app:layout_behavior="@string/appbar_scrolling_view_behavior" />
          </LinearLayout>
      </android.support.design.widget.CoordinatorLayout>
      

      在你的styles.xml中添加appbar主题

         <style name="appBar" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
          <item name="android:textColorPrimary">@android:color/black</item>
          <item name="android:textColorSecondary">@android:color/white</item>
      
      </style>
      

      【讨论】:

      • 这解决了触摸问题,它还添加了与工具栏相同的背景,但仍不包括在内。
      • 查看我编辑的帖子,我已经编辑了 appBar 主题,只需在 styles.xml 中添加
      • 它没有按我想要的方式工作...你能开始一个聊天室吗请让我再给你发一张我现在得到的截图
      • 哦,你在 1 小时后成功了,我将在线,你的 java 代码有问题
      猜你喜欢
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多