简体   繁体   中英

How to set view as a content of TabSpec?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TabHost tabHost = new TabHost(this);
    tabHost.setId(2);
    TabWidget tabWidget = new TabWidget(this);
    tabWidget.setId(android.R.id.tabs);
    tabHost.addView(tabWidget, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    FrameLayout tabcontentFrame = new FrameLayout(this);
    tabcontentFrame.setId(android.R.id.tabcontent);

    tabHost.addView(tabcontentFrame, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);


    tabHost.setup();

    LinearLayout lay = new LinearLayout(this);
    lay.setId(1);

...

    TabSpec spec = tabHost.newTabSpec(" ");

    spec.setContent(lay.getId()); 
    spec.setIndicator(" ");
    tabHost.addTab(spec);

    //mGame.addView(lay);
    setContentView(tabHost);
}

I want to add a view(LinearLayout) to TabSpec , but I'm getting error in line: spec.setContent(lay.getId()); Error: Could not create tab content because could not find view with id 1 .

I don't want to use any xml. How to make this view visible when adding to TabSpec

/// EDIT

Actually I found a problem. LinearLayout lay should be added firstly to tabcontentFrame , but it still not working and I'm getting next RunTimeException , with cause Resources$NotFoundException with message Unable to start activity ComponentInfo{com.example.konsolatrx/konsolatrx.TrxConsole}: android.content.res.Resources$NotFoundException: Resource ID #0x0

The error is in line tabHost.addTab(spec);

/// SOLUTION

Ok, i found answer.

Problem is in line:

TabHost tabHost = new TabHost(this);

Should be:

TabHost tabHost = new TabHost(this, null);

Attribute set must be specified, but I don't know why. Maybe someone can explain. NKN code is good, but is not solution of this problem, just other way to make that I want.

I use something like this:

// Being th the TabHost
final TabSpec setContent = th.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
  public View createTabContent(String tag) {
    final View ll_view = LayoutInflater.from(globvars.getContext()).inflate(R.layout.tabs_content, null);
    final TextView view = (TextView) ll_view.findViewById(R.id.tabsContent);

    // do all the stuff you need to do to your view

    return ll_view;
  }
});


th.addTab(setContent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM