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

发布于 2024-12-08 05:03:51 字数 3905 浏览 0 评论 0原文

我尝试创建一个选项卡式 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-12-15 05:03:51

您应该在 TabHost 中使用 LinearLayout 而不是 RelativeLayout。还要为您的 TabWidget 设置 android:layout_weight="0"

You should use a LinearLayout instead of your RelativeLayout inside your TabHost. Also set the android:layout_weight="0" for your TabWidget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文