l 不同设备内有RelativeLayout问题

Posted

技术标签:

【中文标题】l 不同设备内有RelativeLayout问题【英文标题】:l have RelativeLayout Problem within different devices 【发布时间】:2020-04-29 08:17:09 【问题描述】:

以下是我的 recyclerView 单元格 xml 布局及其显示方式的屏幕截图 它在某些设备上显示对齐,但在其他设备上显示未对齐,并且未显示 list_consultation_section_two 并且 list_consultation_section_three 无缘无故居中 任何想法为什么会发生这种情况?

<?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/list_certificate_item_parent"
    android:layout_
    android:layout_
    android:layout_marginBottom="@dimen/dimen_4dp"
    android:background="@drawable/layout_bg">
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/list_consultation_section_one"
                android:layout_
                android:layout_
                android:layout_margin="@dimen/dimen_8dp">
                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/consultation_image_view"
                    android:layout_
                    android:layout_
                    android:src="@drawable/ic_user_default_image_icon"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
                <ImageView
                    android:id="@+id/consultation_over_image_view"
                    android:layout_
                    android:layout_
                    android:layout_marginBottom="@dimen/dimen_16dp"
                    android:src="@drawable/ic_consultation_over_ar"
                    app:layout_constraintBottom_toBottomOf="@+id/consultation_image_view"
                    app:layout_constraintEnd_toEndOf="@+id/consultation_image_view"
                    app:layout_constraintStart_toStartOf="@+id/consultation_image_view"
                    app:layout_constraintTop_toTopOf="parent" />
                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/consultation_status_online_image_view"
                    android:layout_
                    android:layout_
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:src="@drawable/ic_consultation_online"
                    app:layout_constraintBottom_toBottomOf="@+id/consultation_image_view"
                    app:layout_constraintStart_toStartOf="@+id/consultation_image_view" />
                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/consultation_status_offline_image_view"
                    android:layout_
                    android:layout_
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:src="@drawable/ic_consultation_offline"
                    app:layout_constraintBottom_toBottomOf="@+id/consultation_image_view"
                    app:layout_constraintStart_toStartOf="@+id/consultation_image_view" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        <RelativeLayout
            android:id="@+id/list_consultation_section_two"
            android:layout_
            android:layout_
            android:layout_centerVertical="true"
            android:layout_toStartOf="@id/list_consultation_section_three"
            android:layout_toEndOf="@+id/list_consultation_section_one">
            <TextView
                android:id="@+id/my_consultation_doctor_name_text_view"
                style="@style/subtitle1Style"
                android:layout_
                android:layout_
                android:layout_marginStart="@dimen/dimen_8dp"
                android:layout_marginTop="@dimen/dimen_8dp"
                android:layout_marginEnd="@dimen/dimen_8dp"
                android:text="@string/dr"
                android:textColor="@color/blue_174c6c" />
            <TextView
                android:id="@+id/my_consultation_doctor_specialty_text_view"
                style="@style/Body2Style"
                android:layout_
                android:layout_
                android:layout_below="@+id/my_consultation_doctor_name_text_view"
                android:layout_marginStart="@dimen/dimen_8dp"
                android:layout_marginEnd="@dimen/dimen_8dp"
                android:text="استشارى اطفال"
                android:textColor="@color/gray_aaacad" />
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/list_consultation_section_three"
            android:layout_
            android:layout_
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true">
            <TextView
                android:id="@+id/my_consultation_date"
                style="@style/Body2Style"
                android:layout_
                android:layout_
                android:layout_marginStart="@dimen/dimen_4dp"
                android:layout_marginEnd="@dimen/dimen_12dp"
                android:text="2/11/2019"
                android:layout_centerHorizontal="true"
                android:textColor="@color/gray_aeb1b1" />
            <TextView
                android:id="@+id/my_consultation_messages_number"
                android:layout_
                android:layout_
                android:layout_below="@+id/my_consultation_date"
                android:layout_marginStart="@dimen/dimen_4dp"
                android:layout_marginTop="@dimen/dimen_4dp"
                android:layout_marginEnd="@dimen/dimen_12dp"
                android:background="@drawable/my_consultation_messages_number_bg"
                android:padding="@dimen/dimen_12dp"
                android:textColor="@color/white"
                android:layout_centerHorizontal="true"
                tools:text="0" />
        </RelativeLayout>
</RelativeLayout>

它适用于 Xiaomi Redmi Note 8 (API 28)、OPPO A3fW (API 22)(完全不同的 API 版本) 并且不适用于联想 tab-7504x (API 24)

如果我为 list_consultation_section_three 设置固定大小,我知道它在所有设备上都可以正常工作的解决方案 但我只是想明白为什么? ]

【问题讨论】:

【参考方案1】:

尝试制作 list_certificate_item_parent LinearLayout 而不是 RelativeLayout 并赋予它权重,试试下面的这段代码

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/list_certificate_item_parent"
android:layout_
android:orientation="horizontal"
android:weightSum="5"
android:layout_
android:layout_marginBottom="@dimen/dimen_4dp"
android:background="@drawable/layout_bg">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/list_consultation_section_one"
            android:layout_
            android:layout_weight="1"
            android:layout_
            android:layout_margin="@dimen/dimen_8dp">
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/consultation_image_view"
                android:layout_
                android:layout_
                android:src="@drawable/ic_user_default_image_icon"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
            <ImageView
                android:id="@+id/consultation_over_image_view"
                android:layout_
                android:layout_
                android:gravity="center_vertical"
                android:layout_marginBottom="@dimen/dimen_16dp"
                android:src="@drawable/ic_consultation_over_ar"
                app:layout_constraintBottom_toBottomOf="@+id/consultation_image_view"
                app:layout_constraintEnd_toEndOf="@+id/consultation_image_view"
                app:layout_constraintStart_toStartOf="@+id/consultation_image_view"
                app:layout_constraintTop_toTopOf="parent" />
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/consultation_status_online_image_view"
                android:layout_
                android:layout_
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:src="@drawable/ic_consultation_online"
                app:layout_constraintBottom_toBottomOf="@+id/consultation_image_view"
                app:layout_constraintStart_toStartOf="@+id/consultation_image_view" />
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/consultation_status_offline_image_view"
                android:layout_
                android:layout_
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:src="@drawable/ic_consultation_offline"
                app:layout_constraintBottom_toBottomOf="@+id/consultation_image_view"
                app:layout_constraintStart_toStartOf="@+id/consultation_image_view" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    <RelativeLayout
        android:id="@+id/list_consultation_section_two"
         android:layout_
         android:layout_weight="3"
        android:layout_
        android:gravity="center_vertical"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@id/list_consultation_section_three"
        android:layout_toEndOf="@+id/list_consultation_section_one">
        <TextView
            android:id="@+id/my_consultation_doctor_name_text_view"
            style="@style/subtitle1Style"
            android:layout_
            android:layout_
            android:layout_marginStart="@dimen/dimen_8dp"
            android:layout_marginTop="@dimen/dimen_8dp"
            android:layout_marginEnd="@dimen/dimen_8dp"
            android:text="@string/dr"
            android:textColor="@color/blue_174c6c" />
        <TextView
            android:id="@+id/my_consultation_doctor_specialty_text_view"
            style="@style/Body2Style"
            android:layout_
            android:layout_
            android:layout_below="@+id/my_consultation_doctor_name_text_view"
            android:layout_marginStart="@dimen/dimen_8dp"
            android:layout_marginEnd="@dimen/dimen_8dp"
            android:text="استشارى اطفال"
            android:textColor="@color/gray_aaacad" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/list_consultation_section_three"
        android:layout_
        android:layout_weight="1"
        android:layout_
        android:gravity="center_vertical"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true">
        <TextView
            android:id="@+id/my_consultation_date"
            style="@style/Body2Style"
            android:layout_
            android:layout_
            android:layout_marginStart="@dimen/dimen_4dp"
            android:layout_marginEnd="@dimen/dimen_12dp"
            android:text="2/11/2019"
            android:layout_centerHorizontal="true"
            android:textColor="@color/gray_aeb1b1" />
        <TextView
            android:id="@+id/my_consultation_messages_number"
            android:layout_
            android:layout_
            android:layout_below="@+id/my_consultation_date"
            android:layout_marginStart="@dimen/dimen_4dp"
            android:layout_marginTop="@dimen/dimen_4dp"
            android:layout_marginEnd="@dimen/dimen_12dp"
            android:background="@drawable/my_consultation_messages_number_bg"
            android:padding="@dimen/dimen_12dp"
            android:textColor="@color/white"
            android:layout_centerHorizontal="true"
            tools:text="0" />
    </RelativeLayout>

【讨论】:

以上是关于l 不同设备内有RelativeLayout问题的主要内容,如果未能解决你的问题,请参考以下文章

[JXOI2018]游戏

HDU - 5919 Sequence II

bzoj 2120

牛客 - Alice and Bob(尺取+二分)

数学三分法

HDU 5919 Sequence II ( 主席树 )