安卓软键盘按键隐藏了视图的内容

4

我在使用安卓软件带有软按键的设备中遇到了布局过大的问题:

总结一下,我的问题是:为什么将布局配置为"match_parent"后,其视图边界会延伸到“真正”的底部窗口边界而不是仅在软按键上方?

现在来说说我的具体问题:

当在任何带有软按键的设备上显示RelativeLayout(如果需要知道,在Fragment中),其中的一个View配置为layout_alignParentBottom="true",则该View会显示在软按键"后面"。

没有软按键的设备上View的图像(应该这样)

带有软按键设备上View的图像(按钮被软按键“隐藏”)

RelativeLayout看起来像这样:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg2">

    <LinearLayout
        android:layout_marginTop="@dimen/settings_container_margin_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <!-- Some views -->

    </LinearLayout>

    <at.eventdrivers.android.ui.MenuButton
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        custom:btnText="@string/..." />

</RelativeLayout>

作为一个对于Android开发和Fragment没有任何经验的新手,这个错误也可能出现在Fragment管理上,所以我也会将其发布:
显示该Fragment的Activity已在AndroidManifest中进行了配置:
<activity
    android:name=".slidingmenu.SlidingHomeActivity"
    android:label="@string/app_name"
    android:logo="@mipmap/ic_launcher"
    android:theme="@style/AppBaseTheme"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

使用的所有FrameLayout都将layout_width和layout_height设置为match_parent。

目前我正在使用一种解决方法,通过编程检查设备是否显示软键按钮,如果是,则将此按钮的边距设置为更高的值:

public static boolean hasSoftKeys(Context c) {
    if(Build.VERSION.SDK_INT <= 10 || (Build.VERSION.SDK_INT >= 14 &&
            ViewConfiguration.get(c).hasPermanentMenuKey())) {
        //menu key is present
        return false;
    } else {
        //No menu key
        return true;
    }
}

那里的问题是,不同设备上的软键按钮高度不同,所以我还需要检查软键按钮的高度(这是可能的,并已在StackOverflow上回答)。我已经被这个问题困扰了几周,非常感谢任何帮助。
2个回答

4
这个方法对我有效,只需将它添加到你的v21/styles.xml文件中即可。
<item name="android:windowDrawsSystemBarBackgrounds">false</item>

我已经研究了这个问题很久,但只有通过您的解决方案才得以解决。非常感谢! - yildirimosman

2
我可以想象三种不同的情况:
  1. (不太可能) 你对 WindowLayoutParams 标志(如 getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);)和/或主题样式进行了更改。

  2. (不太可能。这是我最初的想法,但 layout_alignParentBottom="true" 应该已经为您处理了) 屏幕上没有足够的空间。您可以将您的 RelativeLayout 包装在一个 ScrollView 中(这需要您将 RelativeLayout 的高度更改为 wrap_content),然后查看结果?(如果将 match_parent 设置为 RelativeLayout,它并不意味着它会适合所有内容到屏幕尺寸中。内容很容易溢出屏幕,就像您提供的截图一样。但是,layout_alignParentBottom="true" 可以处理它。)

  3. (最有可能) 在托管 FragmentActivity 中存在一些边距/填充(特别是类似于 android:layout_marginBottom="-XXdp" 的东西)。因此,片段呈现正确,但 Activity 将其移至屏幕下方。

更确切地说,这里需要 @style/AppBaseTheme、Activity 的布局以及如果您对 getWindow() 做了一些可疑的事情-这段代码。

请告诉我是否有帮助!


1
我已经检查了您提供的三种情况,唯一一种可能会干扰片段/活动的方式是在jfeinstein10的SlidingMenu-Library中使用getWindow()。如果选择了SLIDING_WINDOW模式,则会使用它。这可能会更改某些参数,使软键盘“隐藏”某些内容(或者我只是做错了)。 总之,将Sliding-Menu的模式更改为SLIDING_CONTENT可以解决我的问题。 因此,感谢您快速而详细的回答! - Florian Mötz
@FlorianMötz 我也遇到了滑动菜单的问题,但我已经将模式更改为SLIDNG_CONTENT,但并没有解决我的问题,你能帮我吗? - PriyankaChauhan
@PriyankaChauhan,你解决了你的问题吗?我还卡在同样的问题上,请帮帮我。 - Ravindra Kushwaha
public boolean isNavigationBarAvailable() { boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK); boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME); return (!(hasBackKey && hasHomeKey)); } 添加此函数 - PriyankaChauhan
让我们在聊天中继续这个讨论 - PriyankaChauhan
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接