Android: How to build tabs like the ones show on Android UI Page

后端 未结 2 395
生来不讨喜
生来不讨喜 2020-12-31 23:49

So android goes out of its way to build this nice UI guide for everyone to use. But I don\'t see anywhere where it shows code examples of how to build these elements.

2条回答
  •  借酒劲吻你
    2021-01-01 00:22

    do something like this.

    this is a full working code. enjoy

    somewhere in oncreate method of activity extending Tabactivity

      tabHost = getTabHost();
      Intent intent;
      intent = new Intent().setClass(this, FirstActvity.class);
      setupTab("NearBy", intent, R.drawable.firsttabdrawable);
      intent = new Intent().setClass(this, SecondActivity.class);
      setupTab("History", intent, R.drawable.secondtabdrawable);
      intent = new Intent().setClass(this, ThirdActivity.class);
      setupTab("Setting", intent, R.drawable.thirdtabdrawable);
    

    define setupTab methods as

      private void setupTab(String tag, Intent intent, int selectorId) {
      View tabView = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.view, null);
      tabView.setBackgroundResource(selectorId);
      TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent);
      tabHost.addTab(setContent);
      }
    

    view.xml as

    
    
    
    

    and firsttabdrawable.xml in drawable folder as

    
    
       
       
       
       
    
    

    define secondtabdrawable.xml and thirddrawable.xml in the same way

提交回复
热议问题