如何在此ConstraintLayout中定位视图

时间:2018-03-03 17:54:49

标签: android android-layout android-constraintlayout

我希望ImageView位于最右边

看图片第一行有一个白色ImageView如何让它贴在右边。使用Android布局检查器,ImageView显示左右两侧都有空白区域。是否有一些重力留下了我可以使用的东西?

enter image description here

这是XML:

<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="wrap_content"
    android:layout_margin="5dp"
    android:background="?AppTheme.line_box_background_1">

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

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="?AppTheme.list_item_background"
        app:layout_constraintBottom_toTopOf="@+id/guideline567"
        app:layout_constraintEnd_toStartOf="@+id/guideline32"
        app:layout_constraintStart_toStartOf="@+id/guideline67"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1">

        <android.support.constraint.Guideline
            android:id="@+id/guideline5654"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.5"/>

        <ImageView
            android:id="@+id/imageView_extra"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline5654"
            app:srcCompat="@drawable/ic_create_port_place"/>

        <TextView
            android:id="@+id/text_content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:autoSizeTextType="uniform"
            android:textColor="?AppTheme.PrimaryTextColor"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/text_extra"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed"
            tools:layout_constraintBottom_creator="1"
            tools:layout_constraintLeft_creator="1"
            tools:text="some text"/>

        <TextView
            android:id="@+id/text_extra"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:autoSizeTextType="uniform"
            android:gravity="center"
            android:textColor="?AppTheme.PrimaryTextColor"
            app:layout_constraintBottom_toTopOf="@+id/text_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_content"
            tools:layout_constraintBottom_creator="1"
            tools:layout_constraintLeft_creator="1"
            tools:text="some text"/>

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:visibility="invisible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.212"/>

    </android.support.constraint.ConstraintLayout>

    <ImageView
        android:id="@+id/imageView_street_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:contentDescription=""
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toTopOf="@+id/guideline567"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline32"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
        android:id="@+id/imageView_google_place"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:contentDescription=""
        app:layout_constraintBottom_toTopOf="@+id/guideline567"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline32"
        app:layout_constraintTop_toBottomOf="@+id/imageView_street_image"
        app:srcCompat="@drawable/powered_by_google_on_white"/>

    <ImageView
        android:id="@+id/imageView_flag"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline521"
        app:layout_constraintEnd_toStartOf="@+id/guideline67"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_circle"/>

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

    <android.support.constraint.Guideline
        android:id="@+id/guideline567"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.89"/>

    <TextView
        android:id="@+id/text_country"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:autoSizeTextType="uniform"
        android:gravity="center|top"
        android:text="country"
        app:layout_constraintBottom_toTopOf="@+id/guideline567"
        app:layout_constraintEnd_toStartOf="@+id/guideline67"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline521"
        app:layout_constraintVertical_bias="0.050000012"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline521"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.4"/>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

使用ConstraintLayout时,我发现在蓝图模式下预览会更好。如果这样做,您将能够看到指示每个元素中约束的圆圈:

enter image description here

要使用它,只需单击并将箭头拖动到您希望此元素限制为我的视图。在您的情况下,您将使用顶部和右侧约束指南将图像视图定位到屏幕的右上角。

此外,要有一个完美平方的图像视图,你应该将宽度和高度设置为sabe,如果你有一个包装内容和0dp在另一个中它不会被完美地平方而是矩形,因为其中一个允许尺寸自由增长。