我如何在选项卡中添加图标而不是在选项卡适配器中添加图标,它在 android 中扩展了 FragmentPagerAdapter? 我不想在我的项目中使用操作栏
有什么帮助吗??
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
private final int[] icons = {R.drawable.home,R.drawable.buddies,R.drawable.notification,R.drawable.history};
@Override
public CharSequence getPageTitle(int position) {
if(position == 0)
return "Home";
else if(position == 1)
return "Buddies";
else if(position == 2)
return "History ";
else
return "Notifications";
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
HomeFragment home = new HomeFragment();
return home;
case 1:
return new BuddiesFragment();
case 2:
return new HistoryFragment();
case 3:
return new NotificationsFragment();
}
return null;
}
@Override
public int getCount() {
return 4;
}
}
我也试过这个方法,但是没有用
@覆盖 public int getPageIconResId(int position) { 返回图标[位置];
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}
最佳答案
我使用矢量绘图作为我的标签图像,这只适用于 API >21。 但我相信您可以以相同的方式使用图像可绘制对象。
这是我的代码:
class MyPagerAdapter extends FragmentPagerAdapter {
private String[] tabText = getResources().getStringArray(R.array.tabs);
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabText = getResources().getStringArray(R.array.tabs);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if (position == 0)
fragment = new FragmentA();
if (position == 1)
fragment = new FragmentB();
if (position == 2)
fragment=new FragmentC();
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
SpannableString spannableString = null;
if (position == 0) {
//use the MrVector library to inflate vector drawable inside tab
Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_add);
//set the size of drawable to 36 pixels
drawable.setBounds(0, 0, 36, 36);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (position == 1) {
//use the MrVector library to inflate vector drawable inside tab
Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_list);
//set the size of drawable to 36 pixels
drawable.setBounds(0, 0, 36, 36);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (position == 2) {
//use the MrVector library to inflate vector drawable inside tab
Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_settings);
//set the size of drawable to 36 pixels
drawable.setBounds(0, 0, 36, 36);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannableString;
}
@Override
public int getCount() {
return 3;
}
附图片:
@Override
public CharSequence getPageTitle(int position) {
SpannableStringBuilder sb = new SpannableStringBuilder(" ");
if (position == 0) {
Drawable drawable = getDrawable(R.drawable.ic_action_add);
drawable.setBounds(0, 0, 48, 48);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
sb.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (position == 1) {
Drawable drawable = getDrawable(R.drawable.ic_action_list_2);
drawable.setBounds(0, 0, 48, 48);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
sb.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return sb;
}
MrVector 库:https://github.com/telly/MrVector
我用来将 SVG 转换为 VectorDrawable 的工具:http://inloop.github.io/svg2android/
关于android - 将图标添加到扩展 FragmentPagerAdapter 的选项卡适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25990931/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我想这样组织C源代码:+/||___+ext||||___+native_extension||||___+lib||||||___(Sourcefilesarekeptinhere-maycontainsub-folders)||||___native_extension.c||___native_extension.h||___extconf.rb||___+lib||||___(Rubysourcecode)||___Rakefile我无法使此设置与mkmf一起正常工作。native_extension/lib中的文件(包含在native_extension.c中)将被完全忽略。