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

时间:2018-02-09 09:56:37

标签: android multiscreen

我有具有固定高度的回收者视图项目,因此在2560x1440设备上它看起来很酷,但在1280x720上它看起来很可怕。我阅读了谷歌文档,他们说只是为了使用" wrap_content" ," match_parent"并仅使用dp和sp。正如你在下面看到的,我的cardView有500dp的高度。我不能使用" wrap_content"或" match_parent"因为我需要我的buttom留在cardview的中心|我必须做出不同的维度值或类似的东西来解决这个问题吗? 如果您需要屏幕来查看问题,请发表评论,但我认为您了解我的问题

我的项目:

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

我的小尺寸设计:

我需要得到什么:

enter image description here

2 个答案:

答案 0 :(得分:0)

您应该使用public class OrdersActivity : Activity, IOnDateSetListener { private int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day; Button btnDatePicker; //set btnDatePicker's text to be whatever user has selected public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth) { this.year = year; this.month = month; this.day = dayOfMonth; string selectedDate = String.Format(day + "." + month + "." + year); btnDatePicker.Text = selectedDate; } protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Orders); DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day); //set btnDatePicker's text to be DateTime.Now btnDatePicker = FindViewById<Button>(Resource.Id.btnDatePicker); btnDatePicker.Text = DateTime.Now.ToString("dd.MM.yyyy"); btnDatePicker.Click += delegate { datePickerDialog.Show(); }; } 代替linearLayout,以避免混淆项目。但我建议你为小屏幕使用不同的设计,因为它会弄乱你的整个设计

RelativeLayout

答案 1 :(得分:0)

使用ConstraintLayout

build.gradle文件

中添加此依赖项
 compile 'com.android.support.constraint:constraint-layout:1.0.2'

在此处,复制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>