How to set icon to title bar for each Activity in TabLayout

后端 未结 3 884
旧时难觅i
旧时难觅i 2021-01-03 03:01

In my tablayout example, i have created 3 tabs, as usually i set 3 activities for each tab. I can set image to title bar of activity, which adds the intent to each tab. Due

相关标签:
3条回答
  • 2021-01-03 03:24
    --------------------------- One more Action for the same --------------------
    Step Declare instance of class:
    Parent Tab Activity:
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tab_layout_title);
    TabLayoutActivity.tabLayout = this;
    TextView titleText = (TextView) findViewById(R.id.myTitle);
    titleText.setText("Care");
    
    Child Activity
    @Override 
        protected void onResume() {
            super.onResume();
            TabLayoutActivity.tabLayout.titleText.setText("Title name");
    
        }
    
    0 讨论(0)
  • 2021-01-03 03:33

    Hello You cannot use Custom Title feature from activities that are nested in TabHost.

    That is if you are requesting Custom Title from Activity A and Activity B which are nested in TabActivity Android will throw the exception you mentioned above.

    Work around to this issue is to let TabActivity request custom title. And change content of Custom Title of TabActivity from inside the Activity A and Activity B.

    Another tip I can give you is override the onResume() call of Activity A and Activity B to change the TabActivity custom title.

    EDIT : Sample Code

    For your tab activity

    public class TabLayoutDemo extends TabActivity {
    
    //CREATING A PUBLIC STATIC VARIABLE
    public static TabLayoutDemoInstance myTabLayoutDemo;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
    
        TabLayoutDemo.TabLayoutDemoInstance=this;//STORING 
    
        //COMMENTING SET FEATURE IN TAB
        //getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
    //REST CODE REMAINS SAME
    

    Now for your Activity A and B

    public class ActivityA extends Activity{
    //LOST MORE CODE ABOVE
    @Override
    protected void onResume() {
        super.onResume();
    //SET FEATURE FROM INSIDE ACTIVITY
        TabLayoutDemo.TabLayoutDemoInstance.getWindow().
               setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
    }
    

    }

    Copy on resume of ActivityA specified above for B as well.

    Tip: you can change change the title layout from each activity using this.

    I hope that helps.

    0 讨论(0)
  • 2021-01-03 03:40

    The (standard) title you can change easily with

    1. implementing onChildTitleChanged in the TabActivity

      @Override 
      protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
          super.onChildTitleChanged(childActivity, title);
          setTitle(title);
      }
      
    2. set the title in the child activities, f.e.

      @Override 
      protected void onResume() {
          setTitle("Calender");
          super.onResume();
      }
      

    Changing a custom title shouldn't be that hard with this strategy. F.e. all your child-activities could implement a interface and have a method

        public int getTitleResource()
    

    The tab-activity can now do

        @Override 
        protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
            super.onChildTitleChanged(childActivity, title);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, ((TabChild)childActivity).getTitleResource());
        }
    

    Or you can transport the id as part of the child-title-string and parse it back to int...

    0 讨论(0)
提交回复
热议问题