Android ConstraintLayout、子视图的立面和向后兼容性
我有一个Android ConstraintLayout、子视图的立面和向后兼容性,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我有一个ConstraintLayout和一个按钮和一个ImageView作为它的子视图。ImageView放置在按钮上,期望ImageView将绘制在按钮上。(当然,我可以用按钮添加图像作为drawabl。但是,在我的场景中,我想用按钮宽度做一些动画,我希望图像视图保持原样)。但是,图像视图绘制在按钮下方,因为按钮在其默认状态下具有2dp高程。按下按钮时,该高度上升至8dp。通常,我们需要将ImageView的elevation或translationZ属性设置为大于2dp,以使ImageVi
ConstraintLayout
和一个按钮
和一个ImageView
作为它的子视图。ImageView
放置在按钮上,期望ImageView
将绘制在按钮上。(当然,我可以用按钮添加图像作为drawabl。但是,在我的场景中,我想用按钮宽度做一些动画,我希望图像视图保持原样)。但是,图像视图
绘制在按钮
下方,因为按钮
在其默认状态下具有2dp高程。按下按钮时,该高度上升至8dp。通常,我们需要将ImageView
的elevation
或translationZ
属性设置为大于2dp,以使ImageView
显示在按钮上方。但是,在API级别21之前,支持elevation
属性和translationZ
属性。我需要支持API级别19。此外,使用设计库还无法实现标高。在保持条件不变的情况下,是否有任何方法可以在约束视图
内的按钮
上绘制图像视图
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/input_margin_bottom">
<EditText
android:id="@+id/et_account"
style="@style/RocketTheme.EditText"
android:drawableStart="@drawable/ic_bill_pay"
android:hint="@string/prompt_biller_id"
android:inputType="number"
android:maxLength="12"
android:maxLines="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<Button
style="@style/RocketTheme.EditText.SideButton"
android:id="@+id/ib_get_contact"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="Select Biller"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_bill_pay"
android:layout_marginLeft="12dp"
android:elevation="2dp"
android:background="@drawable/bg_white_round"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>
在xml中剪切imageview的代码并将其粘贴到按钮的代码下面。在xml剪切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">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</FrameLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
您应该用框架布局包装您的按钮。它将允许您在下面的视图之上堆积视图。请参见以下示例:
<?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">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</FrameLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
这是我现在要使用的解决方案。根据android文档,后面的兄弟姐妹绘制得更晚。因此,一般来说,如果视图
在布局文件中作为同级出现,它将被绘制在其他同级之上。但是,默认情况下,按钮有一个标高,因此它将绘制在API中标高低于21的视图上方
我们可以在同级中设置高程
或translationZ
,使其显示在按钮
上方。我们还需要确保要放在顶部的视图
稍后放置在布局中。在API低于21的情况下,立面将不起作用,后面的视图将绘制在顶部。这是我现在使用的解决方案。根据android文档,后面的兄弟姐妹绘制得更晚。因此,一般来说,如果视图
在布局文件中作为同级出现,它将被绘制在其他同级之上。但是,默认情况下,按钮有一个标高,因此它将绘制在API中标高低于21的视图上方
我们可以在同级中设置高程
或translationZ
,使其显示在按钮
上方。我们还需要确保要放在顶部的视图
稍后放置在布局中。在低于21的API中,提升将不起作用,随后的视图将绘制在顶部。发布布局xml代码。只需使用FrameLayout包装ImageView发布布局xml代码。只需使用FrameLayout包装ImageView。我查看了文档,根据它,后面的同级将稍后绘制。因此,通常情况下,如果视图稍后作为布局文件中的同级出现,它将在其他同级上绘制。但是,默认情况下,该按钮有一个高程,因此它将绘制在API中高程低于21的视图上方。因此,将ImageView放在下面是行不通的。我查看了文档,根据它,后面的同级是在后面绘制的。因此,通常情况下,如果视图稍后作为布局文件中的同级出现,它将在其他同级上绘制。但是,默认情况下,该按钮有一个高程,因此它将绘制在API中高程低于21的视图上方。因此,将ImageView放在下面不起作用。