【问题标题】:Best way to make responsive Layout for 5" screen sizes in Android Studio在 Android Studio 中为 5" 屏幕尺寸制作响应式布局的最佳方法
【发布时间】:2021-08-04 04:21:54
【问题描述】:

我正在为我的学习开发一个项目,但我犯了一个错误:我只是为我的手机(我使用的是 Pixel 4 XL 型号)和类似产品开发的。

但是,当我在较小的设备(如 Pixel 3)上运行它时,布局非常糟糕。我没有使用约束布局,因为我真的很讨厌它,所以我在整个应用程序中都使用了线性布局。 我不关心平板电脑,只想让它在小型设备(至少 5 英寸)上工作。我读过一些关于创建新布局文件夹的内容,比如 layout-swXXX 但我不确定,我想听听您对最佳方式的看法。

这是我的项目树:

我的布局示例:

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="43dp"
            android:layout_height="53dp"
            app:tint="@color/DarkRed"
            app:srcCompat="@drawable/ic_gps" />

        <TextView
            android:id="@+id/lastLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textStyle="bold"
            android:text="@string/last_position"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="20sp" />

    </LinearLayout>

    <TextView
        android:id="@+id/textLocation"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="@color/LightGrey"
        android:text="@string/no_position_found" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/ic_map"
            android:layout_width="35dp"
            android:layout_height="35dp"
            app:srcCompat="@drawable/ic_map" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp">

            <androidx.cardview.widget.CardView
                android:id="@+id/card_compile_module"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="center"
                android:layout_marginHorizontal="20dp"
                android:foreground="?android:attr/selectableItemBackground"
                app:cardBackgroundColor="#4a4747"
                app:cardCornerRadius="30dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView3"
                        android:layout_width="64dp"
                        android:layout_height="64dp"
                        app:srcCompat="@drawable/ic_module"
                        app:tint="#3acf9f" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center"
                        android:maxLines="2"
                        android:text="@string/compile"
                        android:textColor="@color/white"
                        android:textSize="20dp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/card_download"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="center"
                android:layout_marginHorizontal="20dp"
                android:foreground="?android:attr/selectableItemBackground"
                app:cardBackgroundColor="#4a4747"
                app:cardCornerRadius="30dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView4"
                        android:layout_width="64dp"
                        android:layout_height="64dp"
                        app:srcCompat="@drawable/ic_download_file"
                        app:tint="#3acf9f" />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center"
                        android:maxLines="2"
                        android:text="@string/download"
                        android:textColor="@color/white"
                        android:textSize="20dp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp">>

            <androidx.cardview.widget.CardView
                android:foreground="?android:attr/selectableItemBackground"
                android:id="@+id/card_profile"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="center"
                android:layout_marginHorizontal="20dp"
                app:cardBackgroundColor="#4a4747"
                app:cardCornerRadius="30dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView5"
                        android:layout_width="64dp"
                        android:layout_height="64dp"
                        app:srcCompat="@drawable/ic_user"
                        app:tint="#3acf9f" />

                    <TextView
                        android:id="@+id/textView5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center"
                        android:text="@string/user_settings"
                        android:textColor="@color/white"
                        android:textSize="20dp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:foreground="?android:attr/selectableItemBackground"
                android:id="@+id/card_gps"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="center"
                android:layout_marginHorizontal="20dp"
                app:cardBackgroundColor="#4a4747"
                app:cardCornerRadius="30dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView6"
                        android:layout_width="64dp"
                        android:layout_height="64dp"
                        app:srcCompat="@drawable/ic_gps_settings"
                        app:tint="#3acf9f" />

                    <TextView
                        android:id="@+id/textView6"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center"
                        android:text="@string/gps_settings"
                        android:textColor="@color/white"
                        android:textSize="20dp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:layout_marginTop="100dp"
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:gravity="center"
        android:text="@string/developed_by"
        android:textColor="@color/white"
        android:textSize="20sp" />

</LinearLayout>

【问题讨论】:

    标签: java xml android-studio android-linearlayout


    【解决方案1】:

    最好的方法是约束布局。但是,如果您拒绝使用约束布局进行布局并希望使用线性布局,则绝对不应该使用边距来放置项目。

    如果您使用边距来喜欢居中按钮作为示例,那就不好了。

    由于您没有向我们提供您的代码示例,我不知道您做了什么。但我希望这里能对你有所帮助。

    【讨论】:

    • 已编辑,添加了我的布局之一
    • 谢谢。如我所见,您使用边距将图片放在正确的位置。如果你想要一个图片在中间,尝试使用不同的容器来居中图片。像这样,您的应用会响应更快。
    • 是的,我有一张背景图片,我需要将元素留在该背景的正确位置,所以我使用 marginTop 来移动它们。我应该为较小的设备制作另一种布局吗?
    • 是的,如果你愿意,你可以这样做,但这真的很难。有很多不同的手机。
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 2017-06-30
    • 2012-09-22
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多