怎么做“Android中的Viewpager,底部带有固定标签,每个标签中都有图标和文字”?

时间:2015-10-17 12:10:07

标签: android android-viewpager

如何“ViewPager with bottom Tabs Bar”,这里我提到了图片。

image

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

BottomNavigationView是您要寻找的。

答案 2 :(得分:-1)

是的,您可以在不使用viewpager或不使用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();

}