我正在使用FragmentPagerAdapter,我想用不同的语言设置标题信息(“新闻,信息,帮助,记忆” - 见下面的代码)。问题是如何从res / values / strings.xml中读取标题?
有人可以帮助我吗?
由于
汤姆
public class MainInfoViewPagerAdapter extends FragmentPagerAdapter implements PageIndicator
{
private String[] titles= new String[]
{
"News",
"Information",
"Help",
"Membership"
};
public MainInfoViewPagerAdapter( FragmentManager fm)
{
super(fm);
}
答案 0 :(得分:3)
这就是我解决它的方法:
在适配器中:
public class TitleAdapter extends FragmentPagerAdapter {
private String titles[];
private Fragment frags[];
public TitleAdapter(FragmentManager fm, Context context) {
super(fm);
Resources resources = context.getResources();
titles = resources.getStringArray(R.array.titles);
frags = new Fragment[titles.length];
frags[0] = new NewsFragment();
frags[1] = new ArtistsFragment();
frags[2] = new InfoFragment();
frags[3] = new EatDrinkFragment();
....
/values/arrays.xml看起来像这样:
<string-array name="titles">
<item>NEWS</item>
<item>ARTISTS</item>
<item>INFORMATION</item>
<item>EAT AND DRiNK</item>
</string-array>
如果你想要一种语言,只需添加另一个值文件夹,例如/values-fr/strings.xml,例如法语。
答案 1 :(得分:0)
我管理它创建私有SparseArray<String> TITLES;
并将其填入主Activity中的OnCreate函数,如下所示:
TITLES = new SparseArray<String>();
TITLES.put(KEY_FRAGMENT,
getResources().getString(R.string.subtitle_keys));
TITLES.put(CERTIFICATE_FRAGMENT,
getResources().getString(R.string.subtitle_cert));
TITLES.put(TRUST_NETWORK_FRAGMENT,
getResources().getString(R.string.subtitle_trust_network));
TITLES.put(CRL_FRAGMENT, getResources()
.getString(R.string.subtitle_crl));
TITLES.put(SECURE_FRAGMENT,
getResources().getString(R.string.subtitle_secure));
TITLES.put(SETTINGS_FRAGMENT,
getResources().getString(R.string.subtitle_settings));
最后FragmentPagerAdapter
getTitle
做了类似的事情:
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
// Fragment of the app is the Key Management.
return TITLES.get(position);
case 1:
// Fragment of the app is the Certificate Management.
return TITLES.get(position);
case 2:
// Fragment of the app is the Trust Network Management.
return TITLES.get(position);
case 3:
// Fragment of the app is the CRL Management.
return TITLES.get(position);
default:
return TITLES.get(position);
}
}
也许它不是最好的方式,但它是一种方式!
答案 2 :(得分:0)
你能测试的另一个方法就是这个。对于此测试,我创建了一个带有选项卡的活动,每次用户选择一个选项卡时,操作栏中的字幕都会使用不同的语言(英语/西班牙语)进行更改。所以我猜你的Activity结构是这样的:
MainActivity extends FragmentActivity
- In this class you have one ViewPager and one PagerAdapter as attributes
- PagerAdapter is a inner class inside MainActivity.
代码是这样的,首先是主要活动:
public class TrustNetworkActivity extends FragmentActivity implements
ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
private SparseArray<String> TITLES;
ViewPager mViewPager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DataBaseDictionary.PACKAGE_NAME = getApplicationContext()
.getPackageName();
//Loads the data base
initDBLoader(this);
fillSubtitlesArray();
// Create the adapter that will return a fragment for each of the three
// primary sections
// of the app.
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(
getSupportFragmentManager());
// Set up the action bar.
final ActionBar ab = getSupportActionBar();
// ab.setDisplayHomeAsUpEnabled(Boolean.TRUE);
ab.setSubtitle(TITLES.get(KEY_FRAGMENT));
ab.setDisplayShowTitleEnabled(true);
// Set up the ViewPager, attaching the adapter and setting up a listener
// for when the user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select
// the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if
// we have a reference to the
// Tab.
ab.setSubtitle(TITLES.get(position));
ab.setSelectedNavigationItem(position);
}
});
ab.addTab(ab.newTab().setText("").setIcon(R.drawable.ic_menu_keys)
.setTabListener(this));
ab.addTab(ab.newTab().setText("").setIcon(R.drawable.ic_menu_cert)
.setTabListener(this));
ab.addTab(ab.newTab().setText("")
.setIcon(R.drawable.ic_menu_trust_network).setTabListener(this));
ab.addTab(ab.newTab().setText("").setIcon(R.drawable.ic_menu_crl)
.setTabListener(this));
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
/**
* Fills the subtitle array with the resource strings, so it would be
* multilanguage
*/
private void fillSubtitlesArray() {
TITLES = new SparseArray<String>();
TITLES.put(0,
getResources().getString(R.string.subtitle_keys));
TITLES.put(1,
getResources().getString(R.string.subtitle_cert));
TITLES.put(2,
getResources().getString(R.string.subtitle_trust_network));
TITLES.put(3, getResources()
.getString(R.string.subtitle_crl));
TITLES.put(4,
getResources().getString(R.string.subtitle_secure));
TITLES.put(5,
getResources().getString(R.string.subtitle_settings));
}
正如您在ViewPager的OnPageChangeListener
中看到的那样,我调用了操作栏setSubtitle
方法,因此,每次用户选择新页面时,我的操作栏的字幕都会更改,然后是适配器:
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the primary sections of the app.
*/
public class AppSectionsPagerAdapter extends FragmentStatePagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
// Fragment of the app
return new Fragment1();
case 1:
// Fragment of the app
return new Fragment2();
case 2:
// Fragment of the app
return new Fragment3();
case 3:
// Fragment of the app
return new Fragment4();
default:
return new Fragment5();
}
}
@Override
public int getCount() {
return 4;
}
}
我希望这可以帮到你!