【问题标题】:How to add fixed width image view in center horizontal using constraint layout如何使用约束布局在水平中心添加固定宽度的图像视图
【发布时间】:2018-10-09 12:14:02
【问题描述】:

大家好,我需要使用约束布局将 2 个固定宽度和高度的图像视图添加到屏幕中心,我们如何才能实现这样的效果。

xml 布局

<?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=".ui.activity.profile.view.UserDetailsActivity">

    <TextView
        android:id="@+id/tv_label"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="@string/you_are"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent=".1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_percent=".25" />


    <CircularImageView
        android:id="@+id/iv_farmer"
        android:layout_width="@dimen/_100dp"
        android:layout_height="@dimen/_100dp"
        android:src="@drawable/ic_profilewithimage"
        app:civ_border="true"
        app:civ_border_color="#e4e4e4"
        app:civ_border_width="@dimen/_1dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="@+id/iv_prof"
        app:layout_constraintTop_toBottomOf="@+id/tv_label" />

    <CircularImageView
        android:id="@+id/iv_prof"
        android:layout_width="@dimen/_100dp"
        android:layout_height="@dimen/_100dp"
        android:src="@drawable/ic_profilewithimage"
        app:civ_border="true"
        app:civ_border_color="#e4e4e4"
        app:civ_border_width="@dimen/_1dp"
        app:layout_constraintLeft_toRightOf="@+id/iv_farmer"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_label" />

</android.support.constraint.ConstraintLayout>

我正在尝试这个,但视图没有居中。

【问题讨论】:

  • 嗨,你在哪里坚持创建相同的?在此处添加您的 xml 布局,以便我们为您提供帮助。
  • 添加了 xml 布局

标签: android android-layout android-xml android-constraintlayout


【解决方案1】:

我已经更新了你的 xml 代码。请试试这个。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="You are"
        app:layout_constraintBottom_toTopOf="@+id/iv_farmer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent=".1"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_percent=".25" />


    <CircularImageView
        android:id="@+id/iv_farmer"
        android:layout_width="@dimen/_100dp"
        android:layout_height="@dimen/_100dp"
        android:src="@drawable/ic_profilewithimage"
        app:civ_border="true"
        app:civ_border_color="#e4e4e4"
        app:civ_border_width="@dimen/_1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/iv_prof"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CircularImageView
        android:id="@+id/iv_prof"
        android:layout_width="@dimen/_100dp"
        android:layout_height="@dimen/_100dp"
        android:src="@drawable/ic_profilewithimage"
        app:civ_border="true"
        app:civ_border_color="#e4e4e4"
        app:civ_border_width="@dimen/_1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/iv_farmer"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

希望对你有帮助:)

【讨论】:

  • app:layout_constraintWidth_percent=".25" 的使用真的很有帮助
【解决方案2】:

在您的第一个 CircularImageView 中,您的右约束存在问题:

app:layout_constraintRight_toRightOf="@+id/iv_prof"

这应该限制在iv_prof的左边:

app:layout_constraintRight_toLeftOf="@id/iv_prof"

如果您希望在中心更靠近,则通过将此属性添加到第一个 CircularImageView 来将链的样式更改为 packed

app:layout_constraintHorizontal_chainStyle="packed"

要将Views 在父级中垂直居中,您需要将每个View 的顶部和底部分别约束到父级的顶部和底部。

您还对某些Views 使用左/右约束,对其他人使用开始/结束。尝试在整个布局中只使用其中一组。

【讨论】:

    【解决方案3】:

    使用bias 对我有用:

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
    
        <ImageView
            android:id="@+id/img_splash_logo"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/weather_logo"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 2020-06-09
      • 2019-05-04
      • 1970-01-01
      • 2017-12-22
      • 2019-04-19
      • 2018-03-27
      • 2014-08-09
      • 2014-06-25
      • 2021-08-12
      相关资源
      最近更新 更多