资源字符串作为tabLayout的标题

时间:2016-08-10 09:01:10

标签: java android arrays string

所以我有以下问题: 我想使用字符串数组作为TabLayout的标题。

在这种情况下,我使用ViewPagerAdapter进行布局,现在我想在字符串数组中保存选项卡的标题。

要提供这些标题的翻译,我使用2个资源字符串,但我不能使用它们,因为字符串数组只能使用字符串,而资源字符串是整数(完美句子)。

  

不兼容的类型。

     

必需: java.lang.String

     

找到: int

public class ViewPagerAdapter extends FragmentPagerAdapter {

    String[] tabTitleArray = {
        R.string.dice, R.string.presets
    };

    public ViewPagerAdapter(FragmentManager manager) {

        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return null;
    }

    @Override
    public int getCount() {
        return 0;
    }
}

错误在这里:

   String[] tabTitleArray = {R.string.dice, R.string.presets};

另外:我无法使用getResource().getString();

2 个答案:

答案 0 :(得分:2)

一个明确的解决方案是为ViewPagerAdapter提供Context以访问资源,例如:

ScrollView

然后

public ViewPagerAdapter (FragmentManager manager, Context context) {

    super(manager);
    this.mContext = context;
}

答案 1 :(得分:0)

改为使用字符串数组:

<string-array name="labels">
   <item>my URL1</item>
   <item>my URL2</item>
</string-array>

并使用它:

 final String[] labels = getResources().getStringArray(R.array.labels);