我在 Android 上带有软键按钮的设备上遇到布局太大的问题:
总而言之,我的问题是为什么布局被配置为“匹配父项”其视图边界是否扩展到“真实”底部窗口边界,而不是软键按钮上方?
现在我的具体问题:
使用 View 显示relativelayout(在 Fragment 中,如果有必要的话)布局_alignParentBottom =“真”在任何带有软键按钮的设备上,视图显示在软键按钮“后面”。
没有软键按钮的设备上的视图图像(应该是这样)
带有软键按钮的设备上的视图图像(按钮“隐藏”在软键后面)
相对布局看起来像这样:
<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 做过任何事情,所以错误也可能出现在片段管理中,所以我也将发布此内容:
显示片段的 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>
使用的FrameLayouts都将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 上得到了回答)。
我已经被这个问题困扰了几个星期,非常感谢任何帮助。