【问题标题】:how to do "Viewpager in Android with fixed tabs at bottom with Icon and Text in each tab"?如何做“Android中的Viewpager,底部固定标签,每个标签中都有图标和文本”?
【发布时间】:2018-07-12 22:38:44
【问题描述】:

如何做“带有底部标签栏的 ViewPager”,这里我提到了图像。

【问题讨论】:

    标签: android android-viewpager


    【解决方案1】:

    使用 android 的设计库。

    编译'com.android.support:design:23.0.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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="io.sleeko.board.HomeActivity">
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        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:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            >
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_menu_white_24dp"
                />
    
            <Spinner
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="150dp"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                />
    
        </android.support.v7.widget.Toolbar>
    
    
    </android.support.design.widget.AppBarLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:background="#3202c4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            />
    </RelativeLayout>
    
    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </android.support.design.widget.CoordinatorLayout>
    

    然后在你的 Java 代码中添加这个

     ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    
        tabLayout.setSelectedTabIndicatorHeight(4);
    
        //set drawables for each tab
        tabLayout.getTabAt(0).setIcon(R.drawable.files_tab_drawable);
        tabLayout.getTabAt(1).setIcon(R.drawable.task_tab_drawable);
        tabLayout.getTabAt(2).setIcon(R.drawable.board_tab_drawable);
        tabLayout.getTabAt(3).setIcon(R.drawable.team_tab_drawable);
        tabLayout.getTabAt(4).setIcon(R.drawable.settings_tab_drawable);
    

    【讨论】:

    • 我不知道你在说什么。它必须是可点击的。而且我记不太清了。无论如何,如果您正在做这样的事情,那么最好使用 android.support.design.widget.BottomNavigationView。一定更容易。
    【解决方案2】:

    BottomNavigationView 是您正在寻找的。

    【讨论】:

      【解决方案3】:

      是的,您可以在不带或带 viewpager 的情况下使用 BottomNavigationView。 Please view my answer on stackoverflow.

      这是我的部分回答,更多内容见链接:

      public void addFragment(FragmentManager fragmentManager,
                                     Fragment fragment,
                                     int containerId,boolean isFromHome){
      
          fragmentManager.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
      
          FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
          if(isFromHome){
              fragmentTransaction.replace(containerId,fragment);
          }else{
              fragmentTransaction.add(new HomeFragment(),"Home");
              fragmentTransaction.addToBackStack("Home");
          }
          fragmentTransaction.replace(containerId,fragment).commit();
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-25
        • 2017-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-20
        相关资源
        最近更新 更多