我在更改标题时遇到问题。我可以使用片段更改标题,但每次我点击或更改为另一个片段时标题都没有更改为正确的标题。这是我的代码:
这是我的主要标签
public class TabActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tab, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
profile_menu tab1 = new profile_menu();
return tab1;
case 1:
group_menu tab2 = new group_menu();
return tab2;
case 2:
world_menu tab3 = new world_menu();
return tab3;
case 3:
prize_menu tab4 = new prize_menu();
return tab4;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile ";
case 1:
return "Group ";
case 2:
return "World ";
case 3:
return "Prize";
}
return null;
}
}
public void setActionBarTitle(String title){
getSupportActionBar().setTitle(title);
}
}
这是我的片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.prize_menu, container, false);
getActivity().setTitle("Prize");
return rootView;
}
答案 0 :(得分:0)
在每个片段的onResume中调用getActivity().setTitle("Prize");
答案 1 :(得分:0)
if (getActivity().getActionBar() != null) {
getActivity().getActionBar().setTitle("YourTitle");
}
在每个片段的onViewCreated()
中使用此行将解决您的问题
答案 2 :(得分:0)
我假设您想要在标签更改时更改标题?
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
getSupportActionBar().setTitle("Profile ");
break;
case 1:
getSupportActionBar().setTitle("Group");
break;
case 2:
getSupportActionBar().setTitle("World");
break;
case 3:
getSupportActionBar().setTitle("Prize");
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
答案 3 :(得分:0)
在片段中 以简单的方式设置标题。在您的公共类中添加此方法。 在操作栏中添加带有后退箭头的标题。
public void setPageTitle(String title) {
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>"+title+ "</font>"));
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstantState){
view = inflater.inflate(R.layout.fragment_name,container,false);
setPageTitle("title");
return view;
}
如果您想单独使用标题,请在片段onCreateView()中单独使用此行。但是这段代码不能处理后退箭头。
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title");
答案 4 :(得分:0)
快速做法: 将您的ToolBar声明为静态并从片段中访问:
MainActivity:
public static Toolbar mToolbar;
你片段中的:
mToolbar.setTitle("your title here");