如何在布局Android中匹配高度和宽度1:1

荣载

我在Linearlayout中有一个imageview,我希望它的宽度为fill_parent。我希望它的高度等于宽度最终的大小。例如:

<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1"

    android:scaleType="centerCrop"
    android:layout_margin="2dp"
    tools:ignore="Suspicious0dp" />
</LinearLayout>

但是,它不会在android布局中显示图像。.如果我在代码中强制将imageview的高度更改为100dp,它可以工作,但是我想知道为什么即使我写了constraintDimensionRatio 1,图像的高度也不匹配宽度。 :1 ..

谢谢

Umair

尝试添加weight到中,您imageview将获得height,否则您必须使用用户wrap_content身份height

<LinearLayout 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="wrap_content"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" 
android:scaleType="centerCrop"
android:layout_margin="2dp"
tools:ignore="Suspicious0dp" />
</LinearLayout>

 <!--or if you don't want to use weight then user  height = "wrap_content"-->

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android中获得可用的屏幕宽度和高度

如何在Android中获取弹出菜单的高度和宽度?

如何在Android中获取显示图像的宽度和高度?

Android在相对布局中使按钮为屏幕宽度和高度的1/3

如何在Android中将布局的宽度和高度都设置为百分比?

Xamarin.Forms:如何使用相对布局将视图居中?宽度和高度返回-1

如何在Android中使布局高度等于其宽度?

定义布局Android的宽度和高度

Android 获取 Compose 的布局宽度和高度

如何在WPF中根据窗口的宽度和高度调整控件的宽度和高度

如何在共享代码中访问Android移动设备的屏幕宽度和高度?

如何在图像C#中设置宽度和高度

如何在OpenCV Python中设置框架的高度和宽度

如何在xml中交换ImageView的高度和宽度

如何在Flutter中调整IconButton的大小(高度和宽度)

如何在OpenCV中获取图像的宽度和高度?

如何在自动表中定义宽度和高度

如何在WebRTC中获取远程视频的宽度和高度

如何在js中设置图像对象的高度和宽度

如何在PowerPoint中从LoadImage()指向宽度和高度

如何在Flutter中确定图像的宽度和高度?

如何在指令和组件中获取元素的宽度/高度?

如何在vuetify中填充窗口的高度和宽度

如何在iOS中获取屏幕的宽度和高度?

如何在SwiftUI中按比例设置高度和宽度?

如何在Flutter中更改列的宽度和高度?

如何在AvalonDock中设置高度和宽度

如何在iOS 8中设置UIAlertController的高度和宽度

如何在代码中传递图像的宽度和高度