我在 Android 上带有软键按钮的设备上的布局太大时遇到问题:
总而言之,我的问题是为什么配置为“match_parent”的布局将其视图边界扩展到“真实”底部窗口边界,而不是在软键按钮上方?
现在到我的具体问题:
在任何具有软键按钮的设备上显示具有视图layout_alignParentBottom="true"的 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 上回答了)。
我已经被这个问题困了几个星期了,非常感谢任何帮助。