我有一个图像视图。我希望它的宽度为 fill_parent。我希望它的高度是最终的宽度。例如:
<ImageView
android:layout_width="fill_parent"
android:layout_height="whatever the width ends up being" />
布局文件中是否可以实现类似的功能,而无需创建我自己的视图类?
Thanks
2021 年 7 月 28 日更新为使用 AndroidX 而不是支持库
首先,按照说明确保您的项目已导入 AndroidXhere https://developer.android.com/jetpack/androidx#using_androidx_libraries_in_your_project.
然后将你的图像包裹在一个ConstraintLayout
,及其字段:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />
</androidx.constraintlayout.widget.ConstraintLayout>
See here https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#ratio
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)