【问题标题】:Android Soft-Key-Buttons hide view's contentAndroid Soft-Key-Buttons 隐藏视图的内容
【发布时间】:2016-04-07 06:22:22
【问题描述】:

我在 Android 上带有软键按钮的设备上的布局过大时遇到了问题:

总结一下,我的问题是为什么配置为 "match_parent" 的布局将其视图边界扩展到“真实”底部窗口​​边界,而不是在软键按钮上方?

现在是我的具体问题:

在任何带有软键按钮的设备上显示具有视图 layout_alignParentBottom="true" 的 RelativeLayout(在片段中,如果有必要知道的话),视图显示在软键按钮的“后面”。

Image of the View on a Device without Soft-Key-Buttons (as it should be)

Image of the View on a Device with Soft-Key-Buttons (the Button is "hiding" behind the Soft-Keys)

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 开发的新手,之前也没有对 Fragments 做过任何事情,因此错误也可能出现在 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 上回答了)。

我已经被这个问题困扰了几个星期,非常感谢任何帮助。

【问题讨论】:

    标签: android android-layout android-fragments softkeys


    【解决方案1】:

    这个对我有用,只需将它添加到您的 v21/styles.xml

    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    

    【讨论】:

    • 我对这个问题研究了很多,但只有你的解决方案才能解决,非常感谢
    【解决方案2】:

    我可以想象三种不同的场景:

    1. (不太可能)您使用LayoutParamsWindow 标志(如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() 做了一些可疑的事情 - 这段代码。

    如果有帮助,请告诉我!

    【讨论】:

    • 我已经检查了你所有的 3 个场景,我唯一一次以某种方式使用 getWindow(),它可能会干扰片段/活动,是在jfeinstein10 的SlidingMenu-Library。如果选择了模式 SLIDING_WINDOW,则使用它。这可能会改变一些参数,所以软键“隐藏”了一些内容(或者我只是做错了)。 总结,将滑动菜单的模式更改为 SLIDING_CONTENT 解决了我的问题。因此,感谢您快速而详细的回答!
    • @FlorianMötz 我的滑动菜单也有同样的问题,但我已将模式更改为 SLIDNG_CONTENT 并不能解决我的问题,请您帮帮我
    • @PriyankaChauhan 你解决了你的问题吗,我遇到了同样的问题..请帮帮我
    • @RavindraKushwaha 我通过在 SlidingMenu.java 中添加一些代码解决了问题
    • @PriyankaChauhan 可以分享一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多