gpt4 book ai didi

java - 在 FragmentPagerAdapter 中设置图标和标题

转载 作者:行者123 更新时间:2023-12-02 02:16:33 25 4
gpt4 key购买 nike

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

这是我完成的代码。

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

还有 Activity

    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

最佳答案

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

关于java - 在 FragmentPagerAdapter 中设置图标和标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49233478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com