设置工具栏标题 [英] Set toolbar title
本文介绍了设置工具栏标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图像这样设置工具栏标题:
I am trying to set my toolbar title like so:
public class StatisticsPage extends Fragment {
public StatisticsPage(){}
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.statistics_pagelayout, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.statsSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.statspage, R.layout.dropdown_item);
spinner.setAdapter(adapter);
getSupportActionBar().setTitle("My title");
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
switch (position) {
case 0:
break;
case 1:
// do whatever stuff you wanna do here
Fragment Fragment_one;
FragmentManager man= getActivity().getSupportFragmentManager();
FragmentTransaction tran = man.beginTransaction();
Fragment_one = new BreweryStatistics();
tran.replace(R.id.main, Fragment_one);//tran.
tran.addToBackStack(null);
tran.commit();
break;
case 2:
Fragment Fragment_two;
FragmentManager mantwo= getActivity().getSupportFragmentManager();
FragmentTransaction trantwo = mantwo.beginTransaction();
Fragment_two = new StyleStatistics();
trantwo.replace(R.id.main, Fragment_two);//tran.
trantwo.addToBackStack(null);
trantwo.commit();
break;
case 3:
Fragment Fragment_three;
FragmentManager manthree= getActivity().getSupportFragmentManager();
FragmentTransaction tranthree = manthree.beginTransaction();
Fragment_three = new TasteStatisticsPage();
tranthree.replace(R.id.main, Fragment_three);//tran.
tranthree.addToBackStack(null);
tranthree.commit();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
String url = "myURL";
String userURLComp = "u=" + userID;
url = url + userURLComp ;
new getBasicStats(getActivity()).execute(url);
return rootView;
}
}
基本上上述代码中的这一行在该片段中不起作用:
Basically this line in the above code does not work in the fragment:
getSupportActionBar().setTitle("My title");
我又如何阻止我的应用标题显示在应用栏中?我还想在设置标题时将其设置为白色.
Also How can I stop my app title from showing up in the app bar? I would also like to make the title white when I can set it.
推荐答案
将其更改为
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home");
您需要告诉活动它是AppCompatActivity
.
然后,由于您处于片段中,因此您可以使用getActivity()
进行主要活动.
Then you use getActivity()
to get the main activity since you are in a fragment.
然后像往常一样设置标题.
Then just set the title as usual.
这篇关于设置工具栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文