Set a title in Toolbar from fragment in Android

后端 未结 17 984
逝去的感伤
逝去的感伤 2021-01-31 03:40

I have been using the latest Toolbar from AppCompatv7 lib.I have placed a textview in the ToolBar ViewGroup And I want to set a title into this Textview from the fragment in my

17条回答
  •  轮回少年
    2021-01-31 04:30

    In your Activity declare the toolbar as public.

    public class YourActivity : Activity
    {
        public Toolbar toolbar;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = .... // initilize your toolbar
        }
    }
    

    Then, from your fragment

    ((YourActivity) getActivity()).toolbar.Title = "Your Title";
    

提交回复
热议问题