Android 不同的屏幕尺寸

Android Different screen size

我做了一个简单的主页仪表板,如何让它适应各种尺寸?我必须多次创建相同的 activity 并调整内容还是有其他方法?

resource

main

使用约束布局代替线性布局,如果布局不符合您的要求,还要检查您在横向模式下的布局,也要在横向模式下对视图进行不同的排列。 https://developer.android.com/training/multiscreen/screensizes

这是修改后的文件(如果有人能用的话)

enter image description here

    <?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:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fcfcfc"
    android:gravity="center"
    tools:context="com.example.homedashboard.MainActivity">

    <androidx.cardview.widget.CardView
        android:id="@+id/bank_card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="46dp"
        android:layout_marginLeft="46dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:layout_constraintBottom_toTopOf="@+id/add_card"
        app:layout_constraintEnd_toStartOf="@+id/ideas_card"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <ImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:background="@drawable/cerclebackgroundpurple"
                android:padding="10dp"
                android:src="@drawable/ic_attach_money_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Banking"
                android:textStyle="bold" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_margin="10dp"
                android:background="@color/lightgray" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Check your bank activities"
                android:textColor="@android:color/darker_gray" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/ideas_card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="20dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:layout_constraintBottom_toTopOf="@+id/links_card"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/bank_card"
        app:layout_constraintTop_toTopOf="parent">

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

            <ImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:background="@drawable/cerclebackgroundpink"
                android:padding="10dp"
                android:src="@drawable/ic_lightbulb_outline_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Ideas"
                android:textStyle="bold" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_margin="10dp"
                android:background="@color/lightgray" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Check your bank activities"
                android:textColor="@android:color/darker_gray" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>


    <androidx.cardview.widget.CardView
        android:id="@+id/add_card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="46dp"
        android:layout_marginLeft="46dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:layout_constraintBottom_toTopOf="@+id/wifi_card"
        app:layout_constraintEnd_toStartOf="@+id/links_card"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bank_card">

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

            <ImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:background="@drawable/cerclebackgroundgreen"
                android:padding="10dp"
                android:src="@drawable/ic_control_point_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Add"
                android:textStyle="bold" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_margin="10dp"
                android:background="@color/lightgray" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Check your bank activities"
                android:textColor="@android:color/darker_gray" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/links_card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:layout_constraintBottom_toTopOf="@+id/wifi_card"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/add_card"
        app:layout_constraintTop_toBottomOf="@+id/ideas_card">

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

            <ImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:background="@drawable/cerclebackgroundyello"
                android:padding="10dp"
                android:src="@drawable/ic_attach_file_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Links"
                android:textStyle="bold" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_margin="10dp"
                android:background="@color/lightgray" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Check your bank activities"
                android:textColor="@android:color/darker_gray" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>


    <androidx.cardview.widget.CardView
        android:id="@+id/wifi_card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="7dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/links_card"
        app:layout_constraintStart_toStartOf="@+id/add_card"
        app:layout_constraintTop_toBottomOf="@+id/add_card">

        <LinearLayout

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

            <ImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:background="@drawable/cerclebackgroundpurple"
                android:padding="10dp"
                android:src="@drawable/ic_wifi_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Wifi"
                android:textStyle="bold" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_margin="10dp"
                android:background="@color/lightgray" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Check your bank activities"
                android:textColor="@android:color/darker_gray" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>