0

在此处输入图像描述

我遇到了这样的问题。

在此处输入图像描述

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

   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;
    }

在此处输入图像描述

但在某些设备上,这导致了不同的问题。例如,对于三星 Galaxy S10+,就会出现这样的问题。我该如何解决这个问题?

清单.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>
4

1 回答 1

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;
    }
于 2019-06-21T07:37:00.820 回答