android - ActionBar 标题不可见

标签 android android-actionbar

我实现了一个操作栏,但它的标题没有显示。此外,我的应用程序有两个 fragment ,标题应根据显示的 fragment 更改。

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());

    } 


}

还请告诉我如何在操作栏中的图标之间添加分隔线。 提前致谢。

最佳答案

尝试改变这一行:

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);

通过这个:

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME, ActionBar.DISPLAY_SHOW_CUSTOM);

关于android - ActionBar 标题不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16937820/

相关文章:

java - 将 YUV_420_888 转换为字节数组

android - ListView 多选 - 默认透明颜色不能正常工作?

android - 是否可以在android上的操作栏上添加一个计时器?

android - 没有相应的 commit() 或 apply() 调用的 SharedPreferences.edit()

android - 用于蓝牙通信的 GATT over SPP 配置文件?

android - 如何从其适配器刷新 listView

android - 将 View 放置在带有叠加层的操作栏下方

android - 如何创建 ActionBar 的 Action Overflow 部分?

android - 操作栏中的 ShareActionProvider 图标

android - 如何将 Activity 中的工具栏背景颜色设置为 colors.xml 文件中的颜色?