首页 > 解决方案 > 如何修复不同屏幕上“TableLayout”内组件的错误显示?

问题描述

我正在为一个应用程序的 MainScreen 进行布局,TableLayout我想在Button里面设置 6 个 square TableLayout(3 行,Button每行 2 个)。
Buttons 应该以相同的间距放置并具有相同的大小。
问题是我不能让它看起来像我想要的那样,因为layout在不同的屏幕分辨率上看起来不同。
我知道这可能是正常行为,但我不知道如何正确定位布局。

是否有任何解决方法而不是为不同的分辨率制作不同的 XML 文件?

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_site_gray"
    android:orientation="vertical"
    tools:context=".MainScreenActivity">


    <TextView
        android:id="@+id/helloText"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:fontFamily="@font/alfa_slab_one"
        android:gravity="center"
        android:text="Witaj "
        android:textColor="@color/mdtp_white"
        android:textSize="23sp" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:paddingEnd="20dp"
        android:paddingStart="20dp">

        <TableRow>


            <RelativeLayout
                android:id="@+id/badge_layout1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingTop="10dp">

                <RelativeLayout
                    android:id="@+id/relative_layout1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/main_button1"
                        android:layout_width="150dp"
                        android:layout_height="150dip"
                        android:gravity="center"
                        android:text="Otrzymane" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/badge_notificationTopLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/relative_layout1"
                    android:background="@drawable/item_count"
                    android:text="37"
                    android:textColor="#FFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/badge_layout2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingTop="10dp">

                <RelativeLayout
                    android:id="@+id/relative_layout2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/main_button2"
                        android:layout_width="150dp"
                        android:layout_height="150dip"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"

                        android:gravity="center"
                        android:text="Zaakceptowane" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/badge_notificationTopRight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/relative_layout2"
                    android:background="@drawable/item_count"
                    android:text="16"
                    android:textColor="#FFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </RelativeLayout>

        </TableRow>

        <TableRow>

            <RelativeLayout
                android:id="@+id/badge_layout3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingTop="10dp">

                <RelativeLayout
                    android:id="@+id/relative_layout3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/main_button3"
                        android:layout_width="150dp"
                        android:layout_height="150dip"
                        android:layout_alignParentBottom="false"
                        android:text="W trakcie" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/badge_notificationCenterLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/relative_layout3"
                    android:background="@drawable/item_count"
                    android:text="0"
                    android:textColor="#FFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/badge_layout4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingTop="10dp">

                <RelativeLayout
                    android:id="@+id/relative_layout4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/main_button4"
                        android:layout_width="150dp"
                        android:layout_height="150dip"
                        android:text="Zakończone"

                        />
                </RelativeLayout>

                <TextView
                    android:id="@+id/badge_notificationCenterRight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/relative_layout4"
                    android:background="@drawable/item_count"
                    android:text="0"
                    android:textColor="#FFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </RelativeLayout>

        </TableRow>


        <TableRow>

            <RelativeLayout
                android:id="@+id/badge_layoutBottomLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingTop="10dp">

                <RelativeLayout
                    android:id="@+id/relative_layout5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/main_button5"
                        android:layout_width="150dp"
                        android:layout_height="150dip"
                        android:text="Zdjęcia" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/badge_notificationBottomLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/relative_layout5"
                    android:background="@drawable/item_count"
                    android:text="0"
                    android:textColor="#FFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/badge_layout6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingTop="10dp">

                <RelativeLayout
                    android:id="@+id/relative_layout6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/main_button6"
                        android:layout_width="150dp"
                        android:layout_height="150dip"
                        android:gravity="center"
                        android:text="Wyjdź" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/badge_notificationBottomRight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/relative_layout6"
                    android:background="@drawable/item_count"
                    android:text="20"
                    android:textColor="#FFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </RelativeLayout>

        </TableRow>

    </TableLayout>

    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/fullListHolder">
    </android.support.constraint.ConstraintLayout>
    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/acceptedListHolder">
    </android.support.constraint.ConstraintLayout>

</RelativeLayout>

结果:

银河 J4+:

银河 J6 2018:

银河 S5:

银河 S7:

标签: androidandroid-layoutscreen-resolution

解决方案


最好使用ConstraintLayout,它非常简单且响应迅速,在您的示例中,您为视图提供了固定尺寸 - 不同的手机有不同的屏幕尺寸,这就是为什么您会在不同的手机上看到不同的显示。

这是您需要的布局示例:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="125dp"
    android:layout_marginEnd="8dp"
    android:text="Button"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button6" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="126dp"
    android:layout_marginEnd="8dp"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="@+id/button7" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="118dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="133dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="@+id/button5" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="424dp"
    android:layout_marginEnd="8dp"
    android:text="Button"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/textView" />

<Button
    android:id="@+id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="423dp"
    android:layout_marginEnd="8dp"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="@+id/textView" />


推荐阅读