ImageView始终位于ConstraintLayout的中心

时间:2018-12-19 16:29:52

标签: android layout android-constraintlayout

仍在学习ConstraintView,所以我希望我缺少明显的东西。问题出在这里:

我想要一个ImageView,它可以在保持其纵横比的同时将其扩大或缩小到尽可能大的视图。切勿剪切图像很重要。

另一个要点是,它被撞到ConstraintLayout的顶部,并且居中的宽度小于高度。

这是布局代码:

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:adjustViewBounds="true"
    app:srcCompat="@drawable/evil_kitten" />

<Button
    android:id="@+id/go_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="Go Kitty!"
    app:layout_constraintBottom_toBottomOf="@+id/imageView2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

但是我得到的是始终在垂直和水平方向居中的图像,这在顶部产生了多余的填充。 (ConstraintLayout的边界为黄色):

图像在保持其宽高比的同时,正确地放大和缩小到尽可能大。但是我该如何将其固定在顶部?

3 个答案:

答案 0 :(得分:1)

不要将您的layout_height设置为match_parent,然后使用layout_constrainedHeight来限制高度不要超出约束布局。

这个想法是相对于父级使用图像视图布局,而不是考虑修改ImageView内部的缩放比例。

<?xml version="1.0" encoding="utf-8"?>
<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"
   tools:context=".MainActivity">

   <ImageView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:src="@drawable/test2"
      app:layout_constrainedHeight="true" />
</android.support.constraint.ConstraintLayout>

答案 1 :(得分:1)

使用此布局。 app:layout_constraintVertical_bias会将ImageView固定在顶部,而android:adjustViewBounds将根据图像尺寸调整ImageView的尺寸。

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

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:src="@drawable/image"
        android:adjustViewBounds="true"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintBottom_toTopOf="@id/button"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginBottom="8dp"
        android:text="Go Kitty!"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

我只是尝试做与您相同的事情,但无法重现该错误,但是无论如何我都会共享布局的代码,因此您可以进行比较:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

<ImageView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/cat"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom">

    <Button android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginStart="100dp"
        android:layout_marginEnd="100dp"
        android:text="Button"/>

</LinearLayout>


</android.support.constraint.ConstraintLayout>

在Android 8.0上有结果: result building in release mode on android 8.0

相关问题