android输入数据时TabHost栏在键盘上方

TabHost bar is above the keyboard when input data in android

在我的应用程序中,我使用 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"

如何在键盘显示时隐藏标签栏?谢谢

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 );
   }
  }

你可以在每个activity你想隐藏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 );  
        }
     }
});