如何确定Galaxy S10 +的软按钮高度?

时间:2019-06-20 08:24:18

标签: android android-layout android-softbuttons

enter image description here

我遇到了这样的问题。

enter image description here

我用以下代码块解决了这个问题。

   public static void fixSoftButtonsHeightProblem(Context context, View view) {
        int softButtonsHeight = Utils.getSoftButtonsBarHeight(context);
        view.setPadding(0, 0, 0, softButtonsHeight);
    }

    public static int getSoftButtonsBarHeight(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager windowManager = ((Activity) context).getWindowManager();
            windowManager.getDefaultDisplay().getMetrics(metrics);
            int usableHeight = metrics.heightPixels;
            windowManager.getDefaultDisplay().getRealMetrics(metrics);
            int realHeight = metrics.heightPixels;
            if (realHeight > usableHeight)
                return realHeight - usableHeight;
            else
                return 0;
        }
        return 0;
    }

enter image description here

但是在某些设备上,这引起了另一个问题。例如对于三星银河S10 +,会出现此问题。我该怎么解决?

Manifest.xml

 <activity
            android:name=".ui.activity.HomeActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan"/>

我的主题样式;

  <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>


    </style>

这种方式遍及整个应用程序。这是我用于HomeActivity的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color"
    android:orientation="vertical"
    tools:context=".ui.activity.MainActivity">


    <com.xxx.app.view.NonSwipeableViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/tablayout"
        android:layout_below="@+id/searchActionBar"
        android:layout_marginTop="-12dp">


    </com.xxx.app.view.NonSwipeableViewPager>


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:tabRippleColor="@null"
        android:background="@drawable/navbar_bg" />

    <ImageView
        android:id="@+id/float_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:src="@drawable/ic_mid_pen"
        android:visibility="visible" />

    <include
        android:id="@+id/searchActionBar"
        layout="@layout/search_actionbar_layout" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我解决了问题。对于可用的屏幕尺寸,此代码块是错误的结果;

  WindowManager windowManager = ((Activity) context).getWindowManager();
            windowManager.getDefaultDisplay().getMetrics(metrics);
            int usableScreenHeight = metrics.heightPixels;

更改

public static void fixSoftButtonsHeightProblem(Context context, View view) {
        int softButtonsHeight = Utils.getSoftButtonsBarHeight(context);
        view.setPadding(0, 0, 0, softButtonsHeight);
    }

    public static int getSoftButtonsBarHeight(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager windowManager = ((Activity) context).getWindowManager();
            windowManager.getDefaultDisplay().getRealMetrics(metrics);
            int realHeight = metrics.heightPixels; //this is full screen size of phone
            View decorView = ((Activity) context).getWindow().getDecorView(); //if you use this in a fragment, use getActivity before getWindow()
            Rect r = new Rect();
            decorView.getWindowVisibleDisplayFrame(r);
            int usableScreenHeight = r.bottom; //this is real usable screen size of phone
            if (realHeight > usableScreenHeight)
                return realHeight - usableScreenHeight;
            else
                return 0;
        }
        return 0;
    }