简体   繁体   English

在 Android Studio 中为 5" 屏幕尺寸制作响应式布局的最佳方法

[英]Best way to make responsive Layout for 5" screen sizes in Android Studio

I'm developing a project for my studies, but I made an error: I developed it just for my phone (I used a Pixel 4 XL model) and similar.我正在为我的学习开发一个项目,但我犯了一个错误:我只是为我的手机(我使用的是 Pixel 4 XL 型号)和类似产品开发的。

But, when I run it on smaller devices (like Pixel 3) the layout is very bad.但是,当我在较小的设备(如 Pixel 3)上运行它时,布局非常糟糕。 I didn't use Constraint Layout because I really hate it, so I went for Linear Layouts all through my whole app.我没有使用约束布局,因为我真的很讨厌它,所以我在整个应用程序中都使用了线性布局。 I don't care about Tablets, just want it to work on small devices (that are at least 5"). I have read something about creating new layout folders, like layout-swXXX but I'm not sure and I want to hear your opinions about the best way to do that.我不关心平板电脑,只想让它在小型设备(至少 5 英寸)上工作。我读过一些关于创建新布局文件夹的内容,比如layout-swXXX但我不确定,我想听听您对最佳方式的看法。

Here is my project tree:这是我的项目树:

在此处输入图像描述

An example of my layout:我的布局示例:

<?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>

The best way to do this would be a Constraint Layout.最好的方法是约束布局。 But if you refuse to make layouts with the Constraint Layout and want to use the Linear Layout you should definitely not work with margins to put items in place.但是,如果您拒绝使用约束布局进行布局并希望使用线性布局,则绝对不应该使用边距来放置项目。

It would be bad, if you use margins to like center a button as an example.如果您使用边距将按钮居中作为示例,那将会很糟糕。

Since you didn't provide us a sample of your code, I don't know what you did.由于您没有向我们提供代码示例,我不知道您做了什么。 But I hope this here may help you a little.但我希望这里能对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM