I tried to create a tabbed Android app, but got stuck with one strangle behavior: the tab content fills the whole screen (ie behind the tabs as well), instead of just underneath the tab. However, when I click different tabs, it does switch the content. So, I guess I just missed a minor tweak. Here is my code (clutter removed). Hope somebody will spot my mistakes.
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("Date").setIndicator("Date").setContent(new Intent(this, DateActivity.class)));
tabHost.addTab(tabHost.newTabSpec("Time").setIndicator("Time").setContent(new Intent(this, TimeActivity.class)));
tabHost.setCurrentTab(0);
int n = tabHost.getTabWidget().getChildCount();
for (int i = 0; i < n; i++)
tabHost.getTabWidget().getChildAt(i).getLayoutParams().height /= 2;
}
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
</TabHost>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="2pt"
android:background="@color/myColor">
<DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TimePicker>
</LinearLayout>
public class DateActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date);
}
public class TimeActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
}
}
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DateActivity"></activity>
<activity android:name=".TimeActivity"></activity>
<activity android:name=".DateTimeActivity"></activity>
</application>
You should use a
LinearLayoutinstead of yourRelativeLayoutinside yourTabHost. Also set theandroid:layout_weight="0"for yourTabWidget.