在FragmentPagerAdapter中设置图标和标题

时间:2018-03-12 10:55:02

标签: java android xml

我想在片段中设置图标和标题,但只能显示标题而不是图标。

这是我的代码。

    package com.amira.amira.amira.Board.Clothes;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;
import android.widget.Toast;

import com.amira.amira.amira.ChatCalendarProfile.Calendar.CalendarFragment;
import com.amira.amira.amira.ChatCalendarProfile.Profile.ProfileFragment;
import com.amira.amira.amira.R;


public class BoardClothesAdapter extends FragmentPagerAdapter {
    Drawable myDrawable;
    String title;
    Context mContext;
    public BoardClothesAdapter(FragmentManager fm, Context mContext) {
        super(fm);
        this.mContext = mContext;
    }


    @Override
    public Fragment getItem(int position) {
        switch (position ) {
            case 0:
            return new HeadFragment();
            case 1:
            return new DressFragment();
            case 2:
            return new JeansFragment();
            case 3:
            return new ShirtFragment();
            case 4:
            return new BalletFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 5;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        SpannableStringBuilder sb = null;
        ImageSpan span;
        switch (position) {
            case 0:
                myDrawable = mContext.getResources().getDrawable(R.mipmap.head);
                title = mContext.getResources().getString(R.string.title_head);
                sb = new SpannableStringBuilder("   " + title);

                myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
                span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
                sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                return sb;

            case 1:
                myDrawable = mContext.getResources().getDrawable(R.mipmap.dress);
                title = mContext.getResources().getString(R.string.title_dress);
                sb = new SpannableStringBuilder("   " + title);
                myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
                span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
                sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                return sb;

            case 2:
                myDrawable = mContext.getResources().getDrawable(R.mipmap.shirt);
                title = mContext.getResources().getString(R.string.title_shirt);
                sb = new SpannableStringBuilder("   " + title);
                myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
                span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
                sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                return sb;

            case 3:
                myDrawable = mContext.getResources().getDrawable(R.mipmap.leg);
                title = mContext.getResources().getString(R.string.title_jeans);
                sb = new SpannableStringBuilder("   " + title);
                myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
                span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
                sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                return sb;

            case 4:
                myDrawable = mContext.getResources().getDrawable(R.mipmap.ballet);
                title = mContext.getResources().getString(R.string.title_ballet);
                sb = new SpannableStringBuilder("   " + title);
                myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
                span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
                sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                return sb;
        }
        return sb;
    }
}

并且有活动

    package com.amira.amira.amira.Board.Clothes;

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

import com.amira.amira.amira.Board.Accessories.BoardAccessoriesActivity;
import com.amira.amira.amira.ChatCalendarProfile.CategoryAdapter;
import com.amira.amira.amira.R;

public class BoardClothesActivity extends AppCompatActivity {
    private TextView textViewClothes;
    private TextView textViewAccessories;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_board_clothes);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        textViewClothes = (TextView)findViewById(R.id.textView_clothes);
        textViewAccessories = (TextView)findViewById(R.id.textView_accessories);
        BoardClothesAdapter adapter = new BoardClothesAdapter(getSupportFragmentManager(),this);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        textViewAccessories.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                startActivity( new Intent(BoardClothesActivity.this, BoardAccessoriesActivity.class));
            }
        });

    }
}

在这张照片中,只显示我的应用程序标题而没有图标。你能不知道如何用图标和标题显示部分页面适配器。

enter image description here

3 个答案:

答案 0 :(得分:3)

Drawable myDrawable;
@Override
        public CharSequence getPageTitle(int position) {
            SpannableStringBuilder sb = null;
            ImageSpan span;
            switch (position) {
                case 0:
                    myDrawable = getResources().getDrawable(R.drawable.Image1);
                    sb = new SpannableStringBuilder("  Page1"); // space added before text for convenience

                    myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
                    span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);
                    sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                    return sb;
                case 1:
                    myDrawable = getResources().getDrawable(R.drawable.Image2);
                    sb = new SpannableStringBuilder("  Page2"); // space added before text for convenience

                    myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
                    span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);
                    sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                    return sb;
                case 2:
                    myDrawable = getResources().getDrawable(R.drawable.Image3);
                    sb = new SpannableStringBuilder("  Page3"); // space added before text for convenience

                    myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
                    span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);
                    sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                    return sb;
            }
            return  sb;
        }

要仅显示标题,请使用以下代码

private String[] title = {
        "shirt",
        "pant",
        "jeans",
    }

     @Override
            public CharSequence getPageTitle(int position) {

                return title [position % title .length];
    }

答案 1 :(得分:0)

首先,检索操作栏,然后您可以修改标题和图标:

    ActionBar bar = ((AppCompatActivity) getSupportActionBar();

    if (bar == null) {
        return;
    }

    // Change title
    bar.setTitle(getResources().getString(R.string.my_title));

    // Change icon
    bar.setIcon(myIcon);

答案 2 :(得分:-1)

在这里找到解决方法

创建这样的图标数组:

private int[] tabIcons = {
        R.drawable.ic_tab_favourite,
        R.drawable.ic_tab_call,
        R.drawable.ic_tab_contacts};`

setIcontabLayout

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);

参考文献: