约束布局导致视图重叠

科迪·哈尼斯

我试图在Android中使用创建视图,ConstraintLayout但是视图重叠或被推离屏幕时遇到很多问题。我认为很多原因可能是因为我已经习惯了iOS约束,并且在考虑这些约束。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".LocationFragment">

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

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

    <TextView style="@style/Label"
        android:id="@+id/currentSelectionLabel"
        android:layout_marginBottom="21dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="11dp"
        android:text="@string/current_selection"
        app:layout_constraintBottom_toTopOf="@id/locationLabel"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView style="@style/Label"
        android:id="@+id/locationLabel"
        android:layout_marginLeft="41dp"
        android:text="@string/none"
        app:layout_constraintBottom_toTopOf="@id/mapView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/currentSelectionLabel"/>

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        app:layout_constraintBottom_toTopOf="@id/previousButton"
        app:layout_constraintDimensionRatio="16:9"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/locationLabel"/>

    <Button style="@style/RoundedButton"
        android:id="@+id/previousButton"
        android:text="@string/previous"
        app:layout_constraintBottom_toTopOf="@id/newButton"
        app:layout_constraintLeft_toLeftOf="@id/guideHorz125"
        app:layout_constraintRight_toRightOf="@id/guideHorz875"
        app:layout_constraintTop_toBottomOf="@id/mapView"/>

    <Button style="@style/RoundedButton"
        android:id="@+id/newButton"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/new_location"
        app:layout_constraintBottom_toTopOf="@id/resetButton"
        app:layout_constraintLeft_toLeftOf="@id/previousButton"
        app:layout_constraintRight_toRightOf="@id/previousButton"
        app:layout_constraintTop_toBottomOf="@id/previousButton"/>

    <Button style="@style/RoundedButton"
        android:id="@+id/resetButton"
        android:text="@string/reset"
        app:layout_constraintBottom_toTopOf="@id/startButton"
        app:layout_constraintLeft_toLeftOf="@id/previousButton"
        app:layout_constraintRight_toRightOf="@id/previousButton"
        app:layout_constraintTop_toBottomOf="@id/previousButton"/>

    <com.company.ui.ImageButton
        android:id="@+id/startButton"
        android:layout_width="240dp"
        android:layout_height="90dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="11dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:src="@drawable/start"
        app:text="@string/get_started"/>
</android.support.constraint.ConstraintLayout>

第一张图片是我想要看到的,第二张图片是我想要的。在此处输入图片说明 在此处输入图片说明

詹斯拉

尝试改变

 <Button style="@style/RoundedButton"
    android:id="@+id/resetButton"
    android:text="@string/reset"
    app:layout_constraintBottom_toTopOf="@id/startButton"
    app:layout_constraintLeft_toLeftOf="@id/previousButton"
    app:layout_constraintRight_toRightOf="@id/previousButton"
    app:layout_constraintTop_toBottomOf="@id/previousButton"/>

 <Button style="@style/RoundedButton"
    android:id="@+id/resetButton"
    android:text="@string/reset"
    app:layout_constraintBottom_toTopOf="@id/startButton"
    app:layout_constraintLeft_toLeftOf="@id/previousButton"
    app:layout_constraintRight_toRightOf="@id/previousButton"
    app:layout_constraintTop_toBottomOf="@id/newButton"/>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章