android - android输入数据时TabHost栏位于键盘上方

标签 android android-layout android-fragments keyboard android-tabhost

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

enter image description here

问题是,当键盘显示时,选项卡栏位于键盘上方,我已经这样设置了输入模式:

android:windowSoftInputMode="stateHidden"

enter image description here

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

最佳答案

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 的 Activity 上添加这段代码,只需传递布局的 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 );  
        }
     }
});

关于android - android输入数据时TabHost栏位于键盘上方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28710609/

相关文章:

android - 重叠图像(扑克牌)

java - Android 库 card.io 在 Moto X 2nd Gen 上跳过摄像头且不扫描卡

android - 新手 : How to set attribute of the relative layout in my case?

android - 在某个位置添加自定义 TextView?

android - 蜂窝 Gmail fragment 转移

android - 避免在创建 Fragment 之前显示 Activity 布局

java - 可变地访问资源 "R.string"

android - 从 Arduino 到 Android 的 Websocket : send integer instead of text

android - EditText setText() 在多个 EditText View 上设置文本

android - ViewPager 内的 ListView 不滚动