[英]ActionBar title is not visible
I have implemented an actionbar but its title is not getting displayed. 我已经实现了一个操作栏,但它的标题没有显示出来。 Also my app has two fragments , the title should change as per the fragment displayed.
此外,我的应用程序有两个片段,标题应根据显示的片段进行更改。
public class MainActivity extends FragmentActivity {
/** Called when the activity is first created. */
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setupActionBar();
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener()
{
public void onPageSelected(int position)
{
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
public void onPageScrollStateChanged(int state)
{
if (state == ViewPager.SCROLL_STATE_DRAGGING)
{
}
if (state == ViewPager.SCROLL_STATE_IDLE)
{
}
}
});
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new AllPatient();
case 1:
//Intent intent = new Intent(MainActivity.this,abc.class);
return new LaunchpadSectionFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
private void setupActionBar() {
ActionBar actionBar = getActionBar();
//actionBar.setDisplayShowHomeEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM);
ViewGroup v = (ViewGroup)LayoutInflater.from(this)
.inflate(R.layout.header, null);
actionBar.setCustomView(v,
new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.RIGHT));
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(getTitle());
}
}
Please also tell me how to add dividers between the icons in actionbar. 还请告诉我如何在操作栏中的图标之间添加分隔符。 Thanks in advance.
提前致谢。
Try changing this line: 尝试更改此行:
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
By this one: 通过这个:
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME, ActionBar.DISPLAY_SHOW_CUSTOM);
I think the problem is that you're adding a custom view with a width of MATCH_PARENT and Android removes the action bar title (but not the home icon) in that case. 我认为问题是你正在添加一个宽度为MATCH_PARENT的自定义视图,在这种情况下Android会删除操作栏标题(但不会删除主页图标)。 It's not very logical, but that's the behavior I see in my app.
这不是很合乎逻辑,但这是我在我的应用中看到的行为。 Setting the width to WRAP_CONTENT creates the same problem.
将宽度设置为WRAP_CONTENT会产生同样的问题。 The solution is to give the custom view a fixed width, or if you need the width to vary from one activity to another, calculate the width programmatically and set it to a new fixed width for each activity.
解决方案是为自定义视图提供固定宽度,或者如果您需要宽度从一个活动到另一个活动,请以编程方式计算宽度并将其设置为每个活动的新固定宽度。
像这样添加show title标志
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.