我可以通过定义多个字符串文件在我的应用程序中支持多种语言,用户可以在其中根据需要切换到适当的语言。更改语言会在所有地方生效,除了选项卡项和TabLayout上方的工具栏上。谢谢
这是我的XML文件的代码
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabTextAppearance="@style/TabTextAppearance"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
这是设置标签标题的方式
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), getResources().getString(R.string.general));
adapter.addFrag(new TwoFragment(), getResources().getString(R.string.crops));
adapter.addFrag(new ThreeFragment(), getResources().getString(R.string.livestock));
adapter.addFrag(new FourFragment(), getResources().getString(R.string.modern_technology));
//adapter.addFrag(new FiveFragment(), "Pests");
viewPager.setAdapter(adapter);
}
答案 0 :(得分:1)
在您的代码中,您应该有一个扩展FragmentPagerAdaper的类。 FragmentPagerAdapter类公开了一个名为“ getPageTitle”的方法(更多信息,请访问https://developer.android.com/reference/android/support/v4/view/PagerAdapter#getPageTitle(int))。重写getPageTitle方法并返回所需的标题。下面的代码
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
Fragment currentFragment = mFragmentList.get(position);
return currentFragment;
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.your_title_0);
case 1:
return getString(R.string.your_title_1);
default:
return getString(R.string.your_title_default)
}
}
}
答案 1 :(得分:0)
通过使用BaseActivity而不是AppCompatActivity扩展TabsActivity获得了预期的结果
public class TabsActivity extends AppCompatActivity
更改为
public class TabsActivity extends BaseActivity