【发布时间】:2019-10-07 12:19:36
【问题描述】:
这是来自RelativeLayout的代码,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:gravity="center">
<ImageView
android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:minWidth="200dp"
android:minHeight="200dp"
android:src="@drawable/ic_baseline_description_24px"
android:tint="@color/grey" />
</RelativeLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txtInputLayoutCaption"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="@dimen/dp8"
android:layout_toStartOf="@+id/imgSend"
android:hint="@string/caption">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextCaption"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/trebuchet" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:id="@+id/imgSend"
android:layout_width="@dimen/dp36"
android:layout_height="@dimen/dp36"
android:layout_alignTop="@+id/txtInputLayoutCaption"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_baseline_send_24px"
android:tint="?attr/colorPrimary" />
</RelativeLayout>
相对布局输出
这里是约束布局代码,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ImageView
android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:minWidth="200dp"
android:minHeight="200dp"
android:src="@drawable/ic_baseline_description_24px"
android:tint="@color/grey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txtInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp8"
android:hint="@string/caption"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imgSend"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:id="@+id/imgSend"
android:layout_width="@dimen/dp36"
android:layout_height="@dimen/dp36"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_baseline_send_24px"
android:tint="?attr/colorPrimary"
app:layout_constraintBottom_toBottomOf="@+id/txtInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/txtInputLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是约束布局代码的输出。
注意:我使用的是androidx.constraintlayout:constraintlayout:2.0.0-beta2
【问题讨论】:
-
如何在 ImageView (imgIcon") 中从 app:layout_constraintBottom_toBottomOf="parent" 更改为 app:layout_constraintBottom_toTopOf="@+id/txtInputLayout"
-
在约束中引用现有视图时删除加号 ("+")。
-
我相信布局只是方程式的一部分。当我将您的基于约束布局的示例粘贴到测试项目中时,它的工作原理和行为(我相信)应该如此 - 也就是说,软 kbd 不与底部重叠,当键盘关闭时一切恢复正常。
-
把
app:layout_constraintBottom_toBottomOf="@+id/txtInputLayout"改成app:layout_constraintBottom_toBottomOf="parent"改成imgSend button -
@MD,我试过了,但结果一样。
标签: android android-constraintlayout android-relativelayout