在ConstraintLayout内部的ScrollView内嵌套LinearLayout最佳实践

时间:2017-10-25 02:59:16

标签: android android-layout android-coordinatorlayout

我有一个布局,我正在使用Constraintlayout,并且它与4个地平线Linearlayout一起正常工作,并且都有自己的子视图。屏幕顶部有一个imageview,底部有一个adview。

在大屏幕上看起来很好,但显然不在小屏幕上,所以我将4个水平的Linearlayout放在ScrollView内的Vertical Linearlayout中。

它的工作正常,但我不确定这是否是完成这项任务的正确方法。我希望为内部地平线Linearlayouts添加一个约束,但必须为宽度匹配父级并设置高度和边距。

这种方法是正确的还是有更好的方法?

<android.support.constraint.ConstraintLayout
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    tools:context="au.com.xxxxxx.xxxxx.Activities.MainActivity">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cropToPadding="false"
        android:scaleType="fitXY"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/fish_in_wave" />

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView">

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

            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp"
                android:orientation="horizontal"
                tools:layout_editor_absoluteX="56dp"
                tools:layout_editor_absoluteY="354dp">

                <ImageButton
                    android:id="@+id/closest"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:background="@color/colorPrimary"
                    android:elevation="10dp"
                    android:paddingBottom="40dp"
                    android:paddingEnd="40dp"
                    android:paddingLeft="40dp"
                    android:paddingRight="40dp"
                    android:paddingStart="40dp"
                    android:paddingTop="40dp"
                    app:srcCompat="@drawable/near_me_white_24dp"
                    tools:layout_editor_absoluteX="35dp"
                    tools:layout_editor_absoluteY="161dp" />

                <Button
                    android:id="@+id/fsNearMe"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:background="@color/colorWhite"
                    android:elevation="10dp"
                    android:text="Show closest fishing spots."
                    android:textAlignment="center"
                    android:textColor="@color/colorBlack"
                    android:textIsSelectable="false"
                    android:textSize="18sp"
                    tools:layout_editor_absoluteX="107dp"
                    tools:layout_editor_absoluteY="256dp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                tools:layout_editor_absoluteX="54dp"
                tools:layout_editor_absoluteY="451dp">


                <ImageButton
                    android:id="@+id/browseby"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:background="@color/colorPrimary"
                    android:elevation="10dp"
                    app:srcCompat="@drawable/map_white_24dp"
                    tools:layout_editor_absoluteX="35dp"
                    tools:layout_editor_absoluteY="255dp" />

                <Button
                    android:id="@+id/fsByState"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/fsNearMe"
                    android:background="@color/colorWhite"
                    android:elevation="10dp"
                    android:text="Browse known fishing spots"
                    android:textAlignment="center"
                    android:textColor="@color/colorBlack"
                    android:textSize="18sp"
                    tools:layout_editor_absoluteX="123dp"
                    tools:layout_editor_absoluteY="244dp" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout3"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                tools:layout_editor_absoluteX="54dp"
                tools:layout_editor_absoluteY="551dp">


                <ImageButton
                    android:id="@+id/addOwn"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:background="@color/colorPrimary"
                    android:elevation="10dp"
                    app:srcCompat="@drawable/add_location_white_24dp"
                    tools:layout_editor_absoluteX="185dp"
                    tools:layout_editor_absoluteY="0dp" />

                <Button
                    android:id="@+id/addMyMap"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/fsByState"
                    android:background="@color/colorWhite"
                    android:elevation="10dp"
                    android:text="Add your own fishing spots"
                    android:textAlignment="center"
                    android:textColor="@color/colorBlack"
                    android:textSize="18sp"
                    tools:layout_editor_absoluteX="107dp"
                    tools:layout_editor_absoluteY="355dp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout4"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_marginBottom="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                tools:layout_editor_absoluteX="67dp"
                tools:layout_editor_absoluteY="629dp">

                <ImageButton
                    android:id="@+id/notes"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:background="@color/colorPrimary"
                    android:elevation="10dp"
                    app:srcCompat="@drawable/note_add_white_24dp"
                    tools:layout_editor_absoluteX="35dp"
                    tools:layout_editor_absoluteY="449dp" />

                <Button
                    android:id="@+id/addMyNotes"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/colorWhite"
                    android:elevation="10dp"
                    android:text="Notes and Pictures"
                    android:textAlignment="center"
                    android:textColor="@color/colorBlack"
                    android:textSize="18sp"
                    tools:layout_editor_absoluteX="0dp"
                    tools:layout_editor_absoluteY="0dp" />

            </LinearLayout>

        </LinearLayout>
    </ScrollView>


    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:adSize="BANNER"
        app:adUnitId="@string/UNIT_ID"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />


</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案