安卓系统 不同的屏幕尺寸

问题描述 投票:0回答:2

我做了一个简单的Home dashboard,如何让它适应各种尺寸,我是要多次创建同一个活动并调整内容还是有其他方法?

资源

android size screen
2个回答
0
投票

使用Constraint Layout代替Linear Layout,并且在横向模式下检查你的布局,如果布局不符合你的要求,也可以在横向模式下为视图做不同的安排。https:/developer.android.comtrainingmultiscreenscreensizes。


0
投票

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

请在此输入图片描述

    <?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>
© www.soinside.com 2019 - 2024. All rights reserved.