Création d'un tabhost dynamique en ligne de code
Bonjour,je suis nouveau dans le développement Android et présentement j’essaye de créer dynamiquement un onglet sous Android. c'est à dire sans utiliser le main.xml de la ressource layout. je rencontre des difficultés . voici le code et je ne sais pas si quelqu'un pourra m'aider
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| package com.onglet;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
TabHost tabs = new TabHost(this);
tabs.setId(android.R.id.tabhost);
main.addView(tabs);
TabWidget tabWidget = new TabWidget(this);
tabWidget.setId(android.R.id.tabs);
tabs.addView(tabWidget);
FrameLayout tabContent = new FrameLayout(this);
tabContent.setId(android.R.id.tabcontent);
tabContent.setPadding(0, 65, 0, 0);
tabs.addView(tabContent);
TextView content = new TextView(this);
content.setText("This is the Frame Content");
content.setId(100);
tabs.setup();
TabSpec tspec1 = tabs.newTabSpec("Tab1");
tspec1.setIndicator("One", this.getResources().getDrawable(android.R.drawable.star_on));
tspec1.setContent(new PreExistingViewFactory(content));
tabs.addTab(tspec1);
TabSpec tspec2 = tabs.newTabSpec("Tab2");
tspec2.setIndicator("Two", this.getResources().getDrawable(android.R.drawable.star_on));
tspec2.setContent(new PreExistingViewFactory(content));
tabs.addTab(tspec2);
TabSpec tspec3 = tabs.newTabSpec("Tab3");
tspec3.setIndicator("Three", this.getResources().getDrawable(android.R.drawable.star_on));
tspec3.setContent(new PreExistingViewFactory(content));
tabs.addTab(tspec3);
setContentView(main);
}
//Makes the content of a tab when it is selected.
class PreExistingViewFactory implements TabContentFactory
{
private final View preExisting;
protected PreExistingViewFactory(View view)
{
preExisting = view;
}
public View createTabContent(String tag)
{
return preExisting;
}
}
} |
ce code a été repris dans ce site et ça semble marcher chez lui
http://android.attemptone.com/layouts/dynamic-tabs/
Merci de votre aide.