【问题标题】:android studio change title using fragmentandroid studio使用片段更改标题
【发布时间】:2017-10-01 02:53:39
【问题描述】:

我在更改标题时遇到问题。我可以使用片段更改标题,但每次单击或更改为另一个片段时,标题都不会更改为正确的标题。这是我的代码:

这是我的主要标签活动

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;
}

【问题讨论】:

标签: android android-fragments


【解决方案1】:

我假设您想在标签更改时更改标题?

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) {

    }
});

【讨论】:

  • 没有工作我的标题卡在奖品上,它根本不想改变。我把它放错地方了吗?我把 @Override protected void onCreate(Bundle savedInstanceState) {
  • @AymeiAymei 是的..对不起我的错误..tq
【解决方案2】:

快速的方法: 将您的 ToolBar 声明为静态并从片段访问:

主活动:

public static Toolbar mToolbar;

在您的片段中:

mToolbar.setTitle("your title here");

【讨论】:

  • 我不知道为什么,但我的应用程序崩溃了
【解决方案3】:

在片段中 以简单的方式设置您的标题。在您的公共类中添加此方法。 在操作栏中添加带有后退箭头的标题。

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

【讨论】:

  • 它仍然无法随机运行。当我单击组片段时,标题更改为个人资料,有时它更改为世界片段
  • 我 100% 确定,我在所有项目中都使用此代码。你有没有放最后一行 ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title");在你的片段 onCreateView 中?
  • 是的,我做了,也许它和我的不匹配,我不知道为什么
  • 您想设置唯一的页面标题还是在操作栏中设置应用名称?
【解决方案4】:
  if (getActivity().getActionBar() != null) {
        getActivity().getActionBar().setTitle("YourTitle");
    }

在每个片段的onViewCreated() 中使用这一行将解决您的问题

【讨论】:

  • 我不知道为什么,但我的标题在我尝试时没有改变
【解决方案5】:

在每个片段的onResume中调用getActivity().setTitle("Prize");

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多