我想根据不同的设备和 dpi 来固定 imageview 的高度。
<ImageView
android:id="@+id/imgFood"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_no_img" />
到目前为止,我将高度固定为 200dp,但我想保持 1:2 的比例而不是固定大小。我知道 android 在 dp 上工作,但我无法弄清楚如何保持所有设备的比率。宽度将与父级匹配(适合设备宽度),但需要相应地更改高度。
有什么想法/建议吗?
最佳答案
你可以像这样动态设置你的高度。
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
ll.getLayoutParams().height = width/2;
ll.getLayoutParams().width = width;
ll.requestLayout();
关于android - 如何根据android中的设备dpi设置与宽度成比例的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35427507/