我在使用安卓软件带有软按键的设备中遇到了布局过大的问题:
总结一下,我的问题是:为什么将布局配置为"match_parent"后,其视图边界会延伸到“真正”的底部窗口边界而不是仅在软按键上方?
现在来说说我的具体问题:
当在任何带有软按键的设备上显示RelativeLayout(如果需要知道,在Fragment中),其中的一个View配置为layout_alignParentBottom="true",则该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上回答)。我已经被这个问题困扰了几周,非常感谢任何帮助。