gpt4 book ai didi

android - 通过手机屏幕大小设置 ImageView 的大小

转载 作者:行者123 更新时间:2023-11-29 00:18:08 27 4
gpt4 key购买 nike

我希望 ImageView 在不同的屏幕尺寸上看起来相同。为了让您更容易理解它的外观,这里有一些图片:

enter image description here

最大的问题是我的 imageViews 是以编程方式创建的。 ImageViews 布局和大小由这段代码设置

LinearLayout LinearLayoutas = (LinearLayout)findViewById(R.id.LinearLayout);

ImageView ImageViewas = new ImageView(this);

ImageViewas.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayoutas.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ImageViewas.setLayoutParams(params);
ImageViewas.getLayoutParams().height = 650;
ImageViewas.getLayoutParams().width = 950;
params.setMargins(0, 50, 0, 0);

关于如何更改此代码以使我的应用在不同屏幕尺寸上看起来相同有什么想法吗?

最佳答案

好吧,您可以检索设备分辨率并设置 imageView 宽度和高度。这是解决方案。

private class ScreenResolution {
int width;
int height;
public ScreenResolution(int width, int height) {
this.width = width;
this.height = height;
}
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
ScreenResolution deviceDimensions() {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
// getsize() is available from API 13
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return new ScreenResolution(size.x, size.y);
}
else {
Display display = getWindowManager().getDefaultDisplay();
// getWidth() & getHeight() are deprecated
return new ScreenResolution(display.getWidth(), display.getHeight());
}
}

..................

LinearLayout LinearLayoutas = (LinearLayout)findViewById(R.id.LinearLayout);

ImageView ImageViewas = new ImageView(this);

ImageViewas.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayoutas.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

ScreenResolution screenRes = deviceDimensions();
ImageViewas.setLayoutParams(params);
ImageViewas.getLayoutParams().height = screenRes.height;
ImageViewas.getLayoutParams().width = screenRes.width;
params.setMargins(0, 50, 0, 0);

另外,当设备方向改变时,宽度和高度也应该交换。您可以在 onConfigurationChanged 中执行此操作:

@Override
public void onConfigurationChanged(Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);
// swap device width and height here and re-assign
}

关于android - 通过手机屏幕大小设置 ImageView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24986703/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com