Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在FragmentPagerAdapter中设置图标和标题_Java_Android_Xml - Fatal编程技术网

Java 在FragmentPagerAdapter中设置图标和标题

Java 在FragmentPagerAdapter中设置图标和标题,java,android,xml,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

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

这是我已经完成的代码

    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));
            }
        });

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


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

    ActionBar bar = ((AppCompatActivity) getSupportActionBar();

    if (bar == null) {
        return;
    }

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

    // Change icon
    bar.setIcon(myIcon);

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

    ActionBar bar = ((AppCompatActivity) getSupportActionBar();

    if (bar == null) {
        return;
    }

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

    // Change icon
    bar.setIcon(myIcon);
要仅显示标题,请使用以下代码

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

     @Override
            public CharSequence getPageTitle(int position) {

                return title [position % title .length];
    }
要仅显示标题,请使用以下代码

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

     @Override
            public CharSequence getPageTitle(int position) {

                return title [position % title .length];
    }
在这里找到答案

创建如下图标阵列:

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

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
参考资料:

在此处查找解决方案

创建如下图标阵列:

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

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
参考资料:


myDrawable=getResources().getDrawable(R.drawable.Image1);myDrawable=mContext.getResources().getDrawable(R.mipmap.dress);不带out mContext not workmyDrawable=getResources().getDrawable(R.drawable.Image1)的getResources()方法;myDrawable=mContext.getResources().getDrawable(R.mipmap.dress);不带mContext not work的getresources()方法欢迎链接到解决方案,但请确保您的答案在没有它的情况下是有用的:这样您的其他用户就会知道它是什么以及为什么存在,然后引用您链接到的页面的最相关部分,以防目标页面不可用。只有一个注意事项:如果您从网站复制粘贴了一些内容,请放入
(blockquotes),这样您就可以避免进一步的问题。欢迎提供指向解决方案的链接,但请确保您的答案在没有它的情况下是有用的:这样您的其他用户就会知道它是什么以及它为什么存在,然后引用你链接到的页面最相关的部分,以防目标页面不可用。只有一个注意事项:如果您从站点复制粘贴了一些内容,请放入
(blockquotes),这样您就可以避免进一步的问题。