我的 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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在
TabHost
中使用LinearLayout
而不是RelativeLayout
。还要为您的TabWidget
设置android:layout_weight="0"
。You should use a
LinearLayout
instead of yourRelativeLayout
inside yourTabHost
. Also set theandroid:layout_weight="0"
for yourTabWidget
.