Reputation: 79
i have this code
package com.example.simgreinirtabs;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Sími");
spec1.setContent(R.id.Sími);
spec1.setIndicator("Sími");
TabSpec spec2=tabHost.newTabSpec("Sms");
spec2.setIndicator("Sms");
spec2.setContent(R.id.Sms);
TabSpec spec3=tabHost.newTabSpec("Net");
spec3.setIndicator("Net");
spec3.setContent(R.id.Net);
TabSpec spec4=tabHost.newTabSpec("Greina");
spec4.setIndicator("Greina");
spec4.setContent(R.id.Greina);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.addTab(spec4);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height =40;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
with this xml file
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/Sími"
android:layout_width="fill_parent"
android:layout_height="340dp"
android:orientation="vertical"
android:paddingTop="60px" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Sms"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Net"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Greina"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
</FrameLayout>
</TabHost>
How can i work with each tab like it is a different xml file. I have 4 tabs, and i want to work with them like i have 4 xml files. Is that possible ? Can i do this another way ?
Upvotes: 0
Views: 988
Reputation: 5817
You should use <include>
tag.
I.e. layout.xml:
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/bScan"
android:layout_below="@+android:id/tabs" >
<include
android:id="@+id/login_tab"
layout="@layout/login" />
<include
android:id="@+id/user_tab"
layout="@layout/user" />
<include
android:id="@+id/rabate_tab"
layout="@layout/rabate" />
<include
android:id="@+id/rules_tab"
layout="@layout/rules" />
</FrameLayout>
Activity:
Context ctx = this.getApplicationContext();
TabSpec tabLogin = mTabHost.newTabSpec("login");
tabLogin.setIndicator("login");
tabLogin.setContent(R.id.login_tab);
TabSpec tabUser = mTabHost.newTabSpec("user");
tabUser.setIndicator("user");
tabUser.setContent(R.id.user_tab);
TabSpec tabRabate = mTabHost.newTabSpec("rabate");
tabRabate.setIndicator("rabate");
tabRabate.setContent(R.id.rabate_tab);
Upvotes: 1