使用ConstraintLayout

时间:2017-08-22 10:08:21

标签: android android-constraintlayout

我读到ConstraintLayout是一个很棒的工具,可用于创建多种屏幕尺寸的应用,例如平板电脑和手机。
但即便如此。布局本身在不同的屏幕尺寸之间缩放(从我看到的),例如,我可以对TextView的文本大小做些什么呢? 我怎样才能使它成比例?
此外,如果你有更好的方法来为手机和平板电脑编写应用程序,也可以随时告诉我(我不希望为所有类型的屏幕创建不同的布局)。

代码示例:

<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.merchantproxy.merchantproxy.activities.DashboardActivity"
    tools:showIn="@layout/activity_dashboard">
<ScrollView
        android:id="@+id/dashboardScrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="157dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1">

        <LinearLayout
            android:id="@+id/dashboardDataLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="1">

                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.CardView
                        android:id="@+id/salesCard"
                        android:layout_width="0dp"
                        android:layout_height="170dp"
                        android:layout_marginBottom="8dp"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="0.05"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="1.0"
                        tools:layout_constraintLeft_creator="1"
                        tools:layout_constraintTop_creator="1"
                        android:layout_marginStart="32dp"
                        android:layout_marginEnd="32dp">


                        <android.support.constraint.ConstraintLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <TextView
                                android:id="@+id/salesTextView"
                                android:layout_width="0dp"
                                android:layout_height="0dp"
                                android:layout_marginBottom="8dp"
                                android:layout_marginEnd="8dp"
                                android:layout_marginLeft="8dp"
                                android:layout_marginRight="8dp"
                                android:layout_marginStart="8dp"
                                android:layout_marginTop="16dp"
                                android:text="Sales"
                                android:textSize="20sp"
                                app:layout_constraintBottom_toTopOf="@+id/salesStats"
                                app:layout_constraintHorizontal_bias="0.0"
                                app:layout_constraintLeft_toLeftOf="parent"
                                app:layout_constraintRight_toRightOf="parent"
                                app:layout_constraintTop_toTopOf="parent" />

                            <android.support.constraint.ConstraintLayout
                                android:id="@+id/salesStats"
                                android:layout_width="0dp"
                                android:layout_height="100dp"
                                android:layout_marginBottom="8dp"
                                android:layout_marginEnd="8dp"
                                android:layout_marginLeft="8dp"
                                android:layout_marginRight="8dp"
                                android:layout_marginStart="8dp"
                                android:layout_marginTop="16dp"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintHorizontal_bias="0.0"
                                app:layout_constraintLeft_toLeftOf="parent"
                                app:layout_constraintRight_toRightOf="parent"
                                app:layout_constraintTop_toTopOf="parent"
                                app:layout_constraintVertical_bias="1.0">

                                <android.support.constraint.Guideline
                                    android:id="@+id/guideline"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:orientation="vertical"
                                    app:layout_constraintGuide_percent="0.33" />

                                <TextView
                                    android:id="@+id/totalSales"
                                    android:layout_width="0dp"
                                    android:layout_height="0dp"
                                    android:layout_marginBottom="40dp"
                                    android:layout_marginLeft="40dp"
                                    android:layout_marginRight="40dp"
                                    android:layout_marginTop="16dp"
                                    android:text="$0"
                                    android:textAlignment="center"
                                    android:textColor="@color/colorSuccess"
                                    android:textSize="35sp"
                                    android:textStyle="bold"
                                    app:layout_constraintBottom_toBottomOf="parent"
                                    app:layout_constraintLeft_toLeftOf="parent"
                                    app:layout_constraintRight_toLeftOf="@+id/guideline"
                                    app:layout_constraintTop_toTopOf="parent" />

                                <LinearLayout
                                    android:id="@+id/netSalesLayout"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginBottom="8dp"
                                    android:layout_marginTop="8dp"
                                    android:orientation="vertical"
                                    app:layout_constraintBottom_toBottomOf="parent"
                                    app:layout_constraintTop_toTopOf="parent"
                                    app:layout_constraintVertical_bias="0.538"
                                    app:layout_constraintLeft_toRightOf="@+id/feesLayout"
                                    android:layout_marginLeft="8dp"
                                    android:layout_marginRight="8dp"
                                    app:layout_constraintRight_toRightOf="parent"
                                    app:layout_constraintHorizontal_bias="0.963"
                                    android:layout_marginStart="8dp"
                                    android:layout_marginEnd="8dp">

                                    <TextView
                                        android:id="@+id/netSales"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:paddingBottom="5dp"
                                        android:text="$0"
                                        android:textAlignment="center"
                                        android:textColor="@color/colorSuccess"
                                        android:textSize="35sp"
                                        android:textStyle="bold"
                                        tools:layout_editor_absoluteX="24dp"
                                        tools:layout_editor_absoluteY="208dp" />

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:text="Net Sales"
                                        android:textAlignment="center"
                                        android:textSize="20sp"
                                        tools:layout_editor_absoluteX="24dp"
                                        tools:layout_editor_absoluteY="208dp" />
                                </LinearLayout>

UI: UI

我提到我只在垂直方向的手机上才有这个问题...如果我将视图旋转到横向,一切都可以扩展

0 个答案:

没有答案