我的Android TabHost控件出了什么问题?

时间:2011-10-04 12:48:04

标签: android android-tabhost

我尝试创建一个带标签的Android应用程序,但却陷入了一种扼杀行为:标签内容填满了整个屏幕(即标签后面),而不仅仅是标签下方。但是,当我单击不同的选项卡时,它会切换内容。所以,我想我只是错过了一个小调整。这是我的代码(杂乱删除)。希望有人能发现我的错误。

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>

1 个答案:

答案 0 :(得分:0)

您应该在LinearLayout内使用RelativeLayout代替TabHost。同时为android:layout_weight="0"设置TabWidget