Set toolbar title

2019-06-15 00:50发布

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.

6条回答
Emotional °昔
2楼-- · 2019-06-15 01:36

The best and easiest way to do this, is to go to Android Manifest and change the label within the Activity section. This is what's responsible for labelling the Activity along the toolbar. This saves you writing lines of code in the class file.

查看更多
不美不萌又怎样
3楼-- · 2019-06-15 01:37

In kotlin

(activity as? AppCompatActivity)?.supportActionBar?.title = "title"

Edit: Added null safe operator ? to avoid NullPointer in case ActionBar not available

查看更多
闹够了就滚
4楼-- · 2019-06-15 01:39

This worked for me inside an activity. I haven't tried it inside fragment.

this.setTitle("welcome")
ActiviryName.this.setTitle("welcome");
查看更多
女痞
5楼-- · 2019-06-15 01:41

Actually you can just do the following:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    getActivity().setTitle("My title");
}

I make the change in onActivityCreated because the Activity may have not been fully initialized yet in onCreateView. In onActivityCreated there is no risk of getting a NullPointerException.

NOTE: I am using an AppCompatActivity with a Toolbar and v4 Fragments associated to it.

查看更多
Ridiculous、
6楼-- · 2019-06-15 01:45

Kotlin set title from resources

Activity:

setTitle(R.string.some_title)

Fragment:

activity?.setTitle(R.string.some_title)
查看更多
放我归山
7楼-- · 2019-06-15 01:48

Change it to

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home");

You needed to tell the activity it is an AppCompatActivity.

Then you use getActivity() to get the main activity since you are in a fragment.

Then just set the title as usual.

查看更多
登录 后发表回答