如何使用选项卡布局设置标题

时间:2017-04-10 07:24:36

标签: android android-viewpager

我正在制作一个实现ViewPager和标签视图的应用,但标题部分为空白。请解释如何为每个fragments设置标题。

我的MainActivity.java

package com.example.android.viewpager;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

/**
 * Displays a {@link ViewPager} where each page shows a different day of the week.
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the view pager that will allow the user to swipe between fragments
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        // Create an adapter that knows which fragment should be shown on each page
        SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager());

        // Set the adapter onto the view pager
        if (viewPager != null) {
            viewPager.setAdapter(adapter);
        }
        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);

        if (tabLayout != null) {
            tabLayout.setupWithViewPager(viewPager);
        }
    }
}

我的SimpleFragmentPagerAdapter类

package com.example.android.viewpager;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Provides the appropriate {@link Fragment} for a view pager.
 */
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {

    public SimpleFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return new MondayFragment();
        } else if (position == 1){
            return new TuesdayFragment();
        }
        else if (position == 2){
            return new WednesdayFragment();
        }
        else if (position == 3){
            return new ThrusdayFragment();
        }
        else {
            return new FridayFragment();
        }
    }


    @Override
    public int getCount() {
        return 5;
    }
}

my activity_main.xml

<LinearLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context="com.example.android.viewpager.MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed" />

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

</LinearLayout>

2 个答案:

答案 0 :(得分:13)

SimpleFragmentPagerAdapter中添加以下代码..包含您的标题字符串

 private String tabTitles[] = new String[]{"Friends", "Suggested Friends", "Status"};



 @Override
 public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }

或者您也可以在您的活动中使用:

TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);

//add tab items with title..
tabLayout.addTab(tabLayout.newTab().setText("Friends"));
tabLayout.addTab(tabLayout.newTab().setText("Suggested Friends"));
tabLayout.addTab(tabLayout.newTab().setText("Status"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

答案 1 :(得分:0)

通过使用以下代码,您的片段名称将被设置为选项卡名称。

String title = getItem(position).getClass().getName();
return title.subSequence(title.lastIndexOf(".") + 1, title.length());