Java settitle does not work properly
•
Android
I created a sliding tab activity and tried to change the title of the application bar dynamically. But everything I did didn't seem to work! This is my mainactivity.java:
public class MainActivity extends AppCompatActivity {
ViewPager mPager;
SlidingTabLayout mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);
mTabs.setDistributeEvenly(true);
mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mTabs.setViewPager(mPager);
}
class MyPagerAdapter extends FragmentPagerAdapter {
int tabIcons[] = {R.drawable.ic_events, R.drawable.ic_clients, R.drawable.ic_history};
String[] tabText = getResources().getStringArray(R.array.tabs);
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
getSupportActionBar().setTitle("Events");
EventsFragment eventsFragment = EventsFragment.getInstance(position);
return eventsFragment;
case 1:
getSupportActionBar().setTitle("Clients");
ClientsFragment clientsFragment = ClientsFragment.getInstance(position);
return clientsFragment;
case 2:
getSupportActionBar().setTitle("History");
HistoryFragment historyFragment = HistoryFragment.getInstance(position);
return historyFragment;
default:
getSupportActionBar().setTitle("Events");
EventsFragment defaultFragment = EventsFragment.getInstance(position);
return defaultFragment;
}
}
@Override
public CharSequence getPageTitle(int position) {
Drawable drawable = getResources().getDrawable(tabIcons[position]);
drawable.setBounds(0,0,64,64);
ImageSpan imageSpan = new ImageSpan(drawable);
SpannableString spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;
}
@Override
public int getCount() {
return 3;
}
}
}
I also tried to set the title of the application bar in the oncreate method using the following method:
getSupportActionBar().setTitle("Test");
It doesn't work either
How can I solve it? thank you!
resolvent:
Call the settitle method on the mainactivity object
Calling from inside mainactivity: settitle (...) only. Calling from another class (mypageradapter): activity. Settitle (...)
An activity is a reference to the mainactivity object. You can use getactivity(), which returns the activity associated with the fragment
Finally, use getactivity(). Settitle (...); To set the title from the clip
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码