![](/img/trans.png)
[英]How to set the icon of each tab in a fragmentpageradapter to a gif animation?
[英]set icon and title in FragmentPagerAdapter
我想在片段中設置圖標和標題,但只能顯示標題而不是圖標。
這是我完成的代碼。
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));
}
});
}
}
在此圖片中,僅在我的應用程序標題中顯示不帶圖標。請問如何顯示帶有圖標和標題的部分頁面適配器。
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];
}
首先,檢索操作欄,然后可以修改標題和圖標:
ActionBar bar = ((AppCompatActivity) getSupportActionBar();
if (bar == null) {
return;
}
// Change title
bar.setTitle(getResources().getString(R.string.my_title));
// Change icon
bar.setIcon(myIcon);
在這里找到解決方法
創建像這樣的圖標數組:
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]);
參考文獻:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.