Ciao, TabWidget ogni scheda si riferiscono a nuovi xml
-
25-09-2019 - |
Domanda
sto usando exmaple di Google di Ciao, TabWidget ma alterato per assomigliare a questo:
main.xml:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="@+layout/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
file java:
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.text));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));
mTabHost.setCurrentTab(0);
}
}
ed ecco il text.xml in res / layout di:
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="This is Tab 1" />
</LinearLayout>
Quello che sto fondamentalmente cercando di fare è di avere ogni scheda fare riferimento al proprio file XML piuttosto che tutti in main.xml, ma il testo nella prima scheda non si presenta.
Soluzione
Stai cercando di avere ogni scheda si riferiscono al proprio Activity
? Se è così, è possibile impostare un intento in quanto il contenuto di ogni scheda:
intent = new Intent().setClass(this, Test.class);
spec = tabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(intent);
tabHost.addTab(spec);
Naturalmente, si sarebbe quindi necessario avere una classe denominata Test (o qualsiasi altra cosa) che i set test.xml
come il layout.
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
}
Questo tutorial vi aiuterà ulteriormente se questo è il tipo di scenario che si sta cercando di creare.
Altri suggerimenti
Ci sono tre opzioni per le schede utilizzando il built-in di accoglienza scheda.
- viste multiple su una singola attività con un file di layout
- Attività multiple
- visualizzazioni personalizzate costruite in uno TabContentFactory
Sembra che si desidera # 3. Quello che dovete fare è qualcosa di simile a questo:
setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag)
{
View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);
// Setup the view here
return view;
}});
View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);
Questo non è giusto come LayoutInflater.inflate accetta due parametri.