15台测试设备中只有1台出现Android活动布局问题(ImageView)

15台测试设备中只有1台出现Android活动布局问题(ImageView),android,android-activity,imageview,android-linearlayout,android-relativelayout,Android,Android Activity,Imageview,Android Linearlayout,Android Relativelayout,我正在测试Android应用程序中的一个新活动。除了一台设备外,所有设备都很好——除了华为P30 Pro 该活动非常简单:它在底部显示一个ImageView和一个小菜单栏。在设置图像之前,将显示一个进度条(旋转圆) 在华为P30 Pro上,一切似乎都正常(ProgressBar出现,圆圈旋转,ProgressBar消失),但没有图像可见。如果按菜单栏中的“应用”按钮转发图像,图像将正确转发,因此它实际上是可用的,只是不会显示在“活动”中 我们在其他几台设备(包括华为P10、P20、几款三星手机和

我正在测试Android应用程序中的一个新活动。除了一台设备外,所有设备都很好——除了华为P30 Pro

该活动非常简单:它在底部显示一个ImageView和一个小菜单栏。在设置图像之前,将显示一个进度条(旋转圆)

在华为P30 Pro上,一切似乎都正常(ProgressBar出现,圆圈旋转,ProgressBar消失),但没有图像可见。如果按菜单栏中的“应用”按钮转发图像,图像将正确转发,因此它实际上是可用的,只是不会显示在“活动”中

我们在其他几台设备(包括华为P10、P20、几款三星手机和HTC)上测试了相同版本的应用程序,一切正常。但在我们提供的两款华为P30 Pro上,都出现了同样的问题

布局代码非常简单:

<LinearLayout 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:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:layout_weight="10">

    <LinearLayout
            android:layout_height="0dp"
            android:layout_width="fill_parent"
            android:layout_weight="9"
            android:orientation="vertical">

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:srcCompat="@tools:sample/avatars"
                    android:contentDescription="Image preview"/>

            <ProgressBar
                    android:id="@+id/indeterminateBar"
                    android:indeterminate="true"
                    android:minWidth="100dp"
                    android:minHeight="100dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerInParent="true"
                    />
        </RelativeLayout>

    </LinearLayout
            android:layout_height="0dp">

    <LinearLayout
            android:layout_height="0dp"
            android:layout_width="fill_parent"
            android:layout_weight="1">
    <!-- bottom menu-bar-code is here as a TableLayout, which works fine on all devices -->
    </LinearLayout>
</LinearLayout>

可能ImageView的高度为0,因此不可见? 我对它可能是什么感到困惑,因为它只是那个特殊的装置

我检查了ImageView drawable是否正确设置(ImageView.getDrawable()!=null),它看起来是正确的(它不是null),以及可见性状态,看起来也不错。
因此,在这一点上,我认为这是一个纯粹的布局问题。

我强烈建议从LinearLayout切换到ConstraintLayout。即使您当前的布局在15台设备中的14台设备上工作,底部菜单的高度始终为10%,这在高度较小的设备上看起来不太好(您是否尝试过横向布局?您会明白我的意思)

一旦切换到ConstraintLayout,15号设备上的布局问题也应该消失

我将您的布局转换为使用ConstraintLayout来显示它的外观。它的内部布局也更少

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:contentDescription="Image preview"
        app:layout_constraintBottom_toTopOf="@id/bottomMenuBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <ProgressBar
        android:id="@+id/indeterminateBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:minWidth="100dp"
        android:minHeight="100dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomMenuBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/bottomMenuBar"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">
        <!-- bottom menu-bar-code is here as a TableLayout, which works fine on all devices -->
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


请注意,由于布局预览中的可见性原因,我在底部菜单的LinearLayout中添加了固定高度。一旦您理解了ConstraintLayout,您还应该为菜单栏更改它。我将其保留为线性布局,以便更方便地使用真实的布局代码。

非常感谢!特别是向我展示已经完成的代码!哇!这确实比我的版本好得多,是的:它在华为P30 Pro上都能工作!你真的救了我一天!非常感谢。