简体   繁体   English

如何正确支持多种屏幕尺寸

[英]How to correctly support multiple screen sizes

I have recycler view item with fixed height, so on 2560x1440 device it looks cool, but on 1280x720 it looks horible.我有固定高度的回收器视图项目,所以在 2560x1440 设备上它看起来很酷,但在 1280x720 上它看起来很糟糕。 I read google docs about that and they say just to use "wrap_content" , "match_parent" and use only dp and sp.我阅读了谷歌文档,他们说只使用 "wrap_content" 、 "match_parent" 并且只使用 dp 和 sp。 As you can see below my cardView has 500dp height.I can't use "wrap_content" or "match_parent" because i need my buttom stay on center|bottom of cardview Do i have to make different dimen values or something like that to fix this problem?正如你在下面看到的,我的 cardView 有 500dp 的高度。我不能使用“wrap_content”或“match_parent”,因为我需要我的屁股保持在 cardview 的中心|底部我是否必须制作不同的尺寸值或类似的东西来解决这个问题问题? If you need screens to see the problem, please leave comment, but i think you understand my problem如果您需要屏幕来查看问题,请发表评论,但我想您了解我的问题

My item:我的项目:

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/transparent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_vertical_margin"
    >

<android.support.v7.widget.CardView
    android:id="@+id/cardView"

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="500dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >


        <ImageView
            android:id="@+id/ticketQrCodeImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/ticketDescriptionLayout"
            />

        <include layout="@layout/view_underline"
            android:layout_below="@+id/ticketQrCodeImage"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
        <RelativeLayout
            android:id="@+id/ticketDescriptionLayout"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/activity_vertical_margin"
            >

            <ImageView
                android:id="@+id/ticketMainImage"
                android:layout_width="110dp"
                android:scaleType="centerCrop"
                android:layout_height="145dp"
                />
            <LinearLayout
                android:layout_toEndOf="@id/ticketMainImage"
                android:layout_marginStart="@dimen/activity_vertical_margin"
                android:layout_width="match_parent"
                android:layout_centerVertical="true"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
                <TextView
                    style="@style/TextViewBlackCustom"
                    android:textSize="@dimen/title_text_size"
                    android:id="@+id/ticketEventName"
                    android:text="Иван Дорн"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />
                <RelativeLayout
                    android:layout_marginTop="5dp"

                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
                    <TextView
                        style="@style/TextViewGrayNotTransCustom"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:text="@string/tickets_date"
                        />
                    <TextView
                        android:id="@+id/ticketDataText"
                        style="@style/TextViewBlackCustom"
                        android:layout_width="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_alignParentEnd="true"
                        android:text="07.10 в 21:00"
                        android:layout_height="wrap_content" />

                </RelativeLayout>
                <RelativeLayout
                    android:layout_marginTop="3dp"

                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
                    <TextView
                        style="@style/TextViewGrayNotTransCustom"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:text="@string/ticket_place"
                        />
                    <TextView
                        android:id="@+id/ticketPlace"
                        style="@style/TextViewBlackCustom"
                        android:layout_width="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_alignParentEnd="true"
                        android:text="Фан-зона"
                        android:layout_height="wrap_content" />

                </RelativeLayout>
                <RelativeLayout
                    android:layout_marginTop="3dp"

                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
                    <TextView
                        style="@style/TextViewGrayNotTransCustom"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:text="@string/ticket_price"
                        />
                    <TextView
                        android:id="@+id/ticketPriceText"
                        style="@style/TextViewBlackCustom"
                        android:layout_width="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_alignParentEnd="true"
                        android:text="500 грн"
                        android:layout_height="wrap_content" />

                </RelativeLayout>


            </LinearLayout>


        </RelativeLayout>


    </RelativeLayout>

</android.support.v7.widget.CardView>



    <Button
        android:id="@+id/ticketReturnBtn"
        style="@style/ButtonCustomRed"
        android:text="@string/tickets_return_ticket_button"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        app:layout_anchorGravity="center|bottom"
        app:layout_anchor="@id/cardView"

        />
</android.support.design.widget.CoordinatorLayout>

What i'm getting on low size devise:我在小尺寸设备上得到了什么:

What i need to get:我需要得到什么:

在此处输入图片说明

You should use linearLayout instead of RelativeLayout to avoid mixing up of items.您应该使用linearLayout而不是RelativeLayout以避免混淆项目。 But I would recommend you to use different design for small screens as it will mess up the whole design of you但是我建议您为小屏幕使用不同的设计,因为它会弄乱您的整个设计

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/transparent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_vertical_margin"
>

<android.support.v7.widget.CardView
android:id="@+id/cardView"

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="500dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >


    <ImageView
        android:id="@+id/ticketQrCodeImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ticketDescriptionLayout"
        />

    <include layout="@layout/view_underline"
        android:layout_below="@+id/ticketQrCodeImage"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />
    <RelativeLayout
        android:id="@+id/ticketDescriptionLayout"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        >

        <ImageView
            android:id="@+id/ticketMainImage"
            android:layout_width="110dp"
            android:scaleType="centerCrop"
            android:layout_height="145dp"
            />
        <LinearLayout
            android:layout_toEndOf="@id/ticketMainImage"
            android:layout_marginStart="@dimen/activity_vertical_margin"
            android:layout_width="match_parent"
            android:layout_centerVertical="true"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                style="@style/TextViewBlackCustom"
                android:textSize="@dimen/title_text_size"
                android:id="@+id/ticketEventName"
                android:text="Иван Дорн"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
             <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="5dp">
                <TextView
                    style="@style/TextViewGrayNotTransCustom"
                      android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                    android:layout_centerVertical="true"
                    android:text="@string/tickets_date"
                    />
                <TextView
                    android:id="@+id/ticketDataText"
                    style="@style/TextViewBlackCustom"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                    android:layout_centerVertical="true"
                    android:layout_alignParentEnd="true"
                    android:text="07.10 в 21:00"
                     />

            </LinearLayout>
             <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp">
                <TextView
                    style="@style/TextViewGrayNotTransCustom"
                      android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"

                    android:text="@string/ticket_place"
                    />
                <TextView
                    android:id="@+id/ticketPlace"
                    style="@style/TextViewBlackCustom"
                      android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                    android:layout_centerVertical="true"
                    android:layout_alignParentEnd="true"
                    android:text="Фан-зона"
                    />

            </RelativeLayout>
            <RelativeLayout
                android:layout_marginTop="3dp"

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
                <TextView
                    style="@style/TextViewGrayNotTransCustom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="@string/ticket_price"
                    />
                <TextView
                    android:id="@+id/ticketPriceText"
                    style="@style/TextViewBlackCustom"
                    android:layout_width="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_alignParentEnd="true"
                    android:text="500 грн"
                    android:layout_height="wrap_content" />

            </RelativeLayout>


        </LinearLayout>


    </RelativeLayout>


</RelativeLayout>

</android.support.v7.widget.CardView>



<Button
    android:id="@+id/ticketReturnBtn"
    style="@style/ButtonCustomRed"
    android:text="@string/tickets_return_ticket_button"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    app:layout_anchorGravity="center|bottom"
    app:layout_anchor="@id/cardView"

    />

Use ConstraintLayout使用约束布局

Add this dependency in your build.gradle filebuild.gradle文件中添加此依赖build.gradle

 compile 'com.android.support.constraint:constraint-layout:1.0.2'

Here, copy the following code in your layout.xml在这里,将以下代码复制到layout.xml

<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"
    android:layout_margin="@dimen/activity_vertical_margin"
    android:background="@android:color/transparent">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/ticketQrCodeImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/ticketDescriptionLayout" />

            <include
                layout="@layout/view_underline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ticketQrCodeImage" />

            <RelativeLayout
                android:id="@+id/ticketDescriptionLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_margin="@dimen/activity_vertical_margin">

                <ImageView
                    android:id="@+id/ticketMainImage"
                    android:layout_width="110dp"
                    android:layout_height="145dp"
                    android:scaleType="centerCrop" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginStart="@dimen/activity_vertical_margin"
                    android:layout_toEndOf="@id/ticketMainImage"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/ticketEventName"
                        style="@style/TextViewBlackCustom"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Иван Дорн"
                        android:textSize="@dimen/title_text_size" />

                    <RelativeLayout
                        android:layout_width="match_parent"

                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp">

                        <TextView
                            style="@style/TextViewGrayNotTransCustom"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:text="@string/tickets_date" />

                        <TextView
                            android:id="@+id/ticketDataText"
                            style="@style/TextViewBlackCustom"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:text="07.10 в 21:00" />

                    </RelativeLayout>

                    <RelativeLayout
                        android:layout_width="match_parent"

                        android:layout_height="wrap_content"
                        android:layout_marginTop="3dp">

                        <TextView
                            style="@style/TextViewGrayNotTransCustom"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:text="@string/ticket_place" />

                        <TextView
                            android:id="@+id/ticketPlace"
                            style="@style/TextViewBlackCustom"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:text="Фан-зона" />

                    </RelativeLayout>

                    <RelativeLayout
                        android:layout_width="match_parent"

                        android:layout_height="wrap_content"
                        android:layout_marginTop="3dp">

                        <TextView
                            style="@style/TextViewGrayNotTransCustom"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:text="@string/ticket_price" />

                        <TextView
                            android:id="@+id/ticketPriceText"
                            style="@style/TextViewBlackCustom"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:text="500 грн" />

                    </RelativeLayout>


                </LinearLayout>


            </RelativeLayout>


        </RelativeLayout>

    </android.support.v7.widget.CardView>


    <Button
        android:id="@+id/ticketReturnBtn"
        style="@style/ButtonCustomRed"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:text="@string/tickets_return_ticket_button"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        app:layout_constraintBottom_toBottomOf="@id/cardView"
        app:layout_constraintLeft_toLeftOf="@id/cardView"
        app:layout_constraintRight_toRightOf="@id/cardView" />
</android.support.constraint.ConstraintLayout>

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

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