在我的应用程序中,我使用tabhost创建如下菜单栏:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
</LinearLayout>
和
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("home").setIndicator("Home"),HomeFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("attraction").setIndicator("Attraction"),AttractionFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("schedule").setIndicator("Schedule"),ScheduleFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("map").setIndicator("Map"),MapViewFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("weather").setIndicator("Weather"),WeatherFragment.class, null);
问题是,当键盘显示时,标签栏位于键盘上方,我已将输入模式设置为:
android:windowSoftInputMode="stateHidden"
键盘显示时如何隐藏标签栏?感谢
答案 0 :(得分:4)
onConfigurationChanged 方法来处理可以使用的运行时更改 tabHost.setVisibility(View.GONE); Handling Runtime Change
// from the link above
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
tabHost.setVisibility( View.GONE );
}
else if (newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_YES) {
tabHost.setVisibility( View.VISIBLE );
}
}
您可以在要隐藏tabhost的每个活动上添加这段代码,只需传递布局的Rootview ID即可。
public final int SOFTKEYBOARDHEIGHT=100;
final View activityRootView = findViewById(R.id.YOURROOTVIEW);
activityRootView.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > SOFTKEYBOARDHEIGHT) {
tabHost.setVisibility( View.GONE );
}
}
});