在 ViewPager Android 中从 String.xml 获取字符串
Get String from String.xml in ViewPager Android
public class DietSlideAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public DietSlideAdapter(Context context){
this.context = context;
}
//Arrays
public int[] slide_images2 = {
R.drawable.breakfast,
R.drawable.cabbage,
R.drawable.protein,
R.drawable.fish,
R.drawable.tea
};
public String [] slide_headings2 = {
"heading1","heading2","heading3","heading4","heading5"
};
public String [] slide_descs2 = {
"Try fortified ready-to-eat or cooked breakfast cereals with fruit. Fortified cereals have added nutrients, like calcium.",
"Choose a variety of vegetables and fruits, like carrots, cooked greens, bananas, and melon.\n" +
"Eat plenty of beans and whole grains. Try brown rice or oatmeal.",
"Iron keeps your blood healthy. Folic acid helps prevent birth defects.\n" +
"Even before you find out you're pregnant, it's smart to start eating plenty of folate-rich foods like fortified cereals, asparagus, lentils, wheat germ, oranges, and orange juice.",
"Avoid fish and shellfish with high levels of mercury. Common fish that are low in mercury include shrimp, and catfish. ",
"Drink decaffeinated coffee or tea.\n" +
"Drink water or seltzer instead of soda.\n" +
"Don't drink alcohol."
};
@Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide2, container, false);
ImageView slideImageView2 = (ImageView) view.findViewById(R.id.slideImage2);
TextView slideHeading2 = (TextView) view.findViewById(R.id.slide_heading2);
TextView slideDescription2 = (TextView) view.findViewById(R.id.slide_desc2);
slideImageView2.setImageResource(slide_images2[position]);
slideHeading2.setText(slide_headings2[position]);
slideDescription2.setText(slide_descs2[position]);
slideDescription2.setMovementMethod(new ScrollingMovementMethod());
container.addView(view);
return view;
}
}
我想让我的应用程序支持多种语言。
我想访问 string.xml 而不是数组 slide_headings2 和 slide_desc2.
中的硬编码字符串
我到处看,我试过 context.getResources().getString(R.string.string_name).
如何用 string.xml 中的 R.string 替换数组中的硬编码字符串?
String[] slide_images2 = {
context.getString(R.string.heading1),
context.getString(R.string.heading2),
context.getString(R.string.heading3),
context.getString(R.string.heading4),
context.getString(R.string.heading5),
};
就这么简单^
public class DietSlideAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public DietSlideAdapter(Context context){
this.context = context;
}
//Arrays
public int[] slide_images2 = {
R.drawable.breakfast,
R.drawable.cabbage,
R.drawable.protein,
R.drawable.fish,
R.drawable.tea
};
public String [] slide_headings2 = {
"heading1","heading2","heading3","heading4","heading5"
};
public String [] slide_descs2 = {
"Try fortified ready-to-eat or cooked breakfast cereals with fruit. Fortified cereals have added nutrients, like calcium.",
"Choose a variety of vegetables and fruits, like carrots, cooked greens, bananas, and melon.\n" +
"Eat plenty of beans and whole grains. Try brown rice or oatmeal.",
"Iron keeps your blood healthy. Folic acid helps prevent birth defects.\n" +
"Even before you find out you're pregnant, it's smart to start eating plenty of folate-rich foods like fortified cereals, asparagus, lentils, wheat germ, oranges, and orange juice.",
"Avoid fish and shellfish with high levels of mercury. Common fish that are low in mercury include shrimp, and catfish. ",
"Drink decaffeinated coffee or tea.\n" +
"Drink water or seltzer instead of soda.\n" +
"Don't drink alcohol."
};
@Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide2, container, false);
ImageView slideImageView2 = (ImageView) view.findViewById(R.id.slideImage2);
TextView slideHeading2 = (TextView) view.findViewById(R.id.slide_heading2);
TextView slideDescription2 = (TextView) view.findViewById(R.id.slide_desc2);
slideImageView2.setImageResource(slide_images2[position]);
slideHeading2.setText(slide_headings2[position]);
slideDescription2.setText(slide_descs2[position]);
slideDescription2.setMovementMethod(new ScrollingMovementMethod());
container.addView(view);
return view;
}
}
我想让我的应用程序支持多种语言。
我想访问 string.xml 而不是数组 slide_headings2 和 slide_desc2.
中的硬编码字符串
我到处看,我试过 context.getResources().getString(R.string.string_name).
如何用 string.xml 中的 R.string 替换数组中的硬编码字符串?
String[] slide_images2 = {
context.getString(R.string.heading1),
context.getString(R.string.heading2),
context.getString(R.string.heading3),
context.getString(R.string.heading4),
context.getString(R.string.heading5),
};
就这么简单^