gpt4 book ai didi

java - 无法使用字符串作为选项卡名称

转载 作者:行者123 更新时间:2023-12-02 04:34:16 26 4
gpt4 key购买 nike

我正在遵循本教程,并希望使用字符串资源作为我的选项卡名称,但显然我在本教程中无法做到这一点。有谁知道这件事吗?

    public class MainActivity extends ActionBarActivity {

// Declaring Your View and Variables

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Home","Events"};
int Numboftabs =2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// Creating The Toolbar and setting it as the Toolbar for the activity

toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);


// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);

// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});

// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
}

Android Sliding Tabs with Material Design | Exoguru

最佳答案

要在 FragmentStatePagerAdapter 中使用 String 资源,您需要一个 Context,因此您需要更改该类的构造函数并传递一个 Context。

public ViewPagerAdapter(FragmentManager fm, int mNumbOfTabsumb, Context context) {
super(fm);
this.NumbOfTabs = mNumbOfTabsumb;
this.context = context;

}

因此,在 getPageTitle 中,您不需要返回 Titles[position]

你可以这样做:

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case FRAGMENT_1:
return context.getResources().getString(R.string.some_string);
}
return super.getPageTitle(position);
}

其中 FRAGMENT_1 是该类的常量。如果你不再使用你的标题,你可以从构造函数中删除它,因为我删除了它。

关于java - 无法使用字符串作为选项卡名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31030580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com