我正在尝试在我的应用中支持多种屏幕尺寸...我将其添加到我的Manifest.xml中:
`<supports-screens
android:normalScreens="true"
android:smallScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>`
我还添加了不同的布局大小和密度:
布局小
布局大
布局正常
layout-xlarge
抽拉-LDPI
提拉 - 华电国际
绘制,MDPI
抽拉-xhpi
我的布局正常当前设置在Nexus 4上:Image here
现在它设置在Nexus One上:Image here
如何将元素设置为自动缩放?
答案 0 :(得分:0)
有没有其他方法可以自动缩放元素而不将其高度和宽度设置为wrap_content或match_parent?
当然你可以通过编程方式完成
例如:
我像这样定义了简单对象ImageSize
:
public class ImageSize {
private float width;
private float height;
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}
然后我创建了一个包含Image(另一个布局)的布局
final LinearLayout sellCarImage = (LinearLayout) findViewById(R.id.sellCarImage);
final RelativeLayout sellCarLayout = (RelativeLayout) findViewById(R.id.sellCarLayout);
然后我计算了宽度和高度,以根据图像实际尺寸缩放视图尺寸:
ImageSize img = new ImageSize();
img = getImageSize(R.drawable.indeximage5, MainActivity.this);
int sellCarWidth = sellCarLayout.getWidth();
int sellCarHeight = (int) ((sellCarWidth * img.getHeight()) / img.getWidth());
并以编程方式设置宽度和高度:
RelativeLayout.LayoutParams sellCarImageIconParams = new RelativeLayout.LayoutParams(sellCarWidth, sellCarHeight);
//sellCarImageIconParams.setMargins(marg, 0, marg, 0);
sellCarImage.setLayoutParams(sellCarImageIconParams);
sellCarImage.setBackgroundResource(R.drawable.indeximage5);
这里是getImageSize
public ImageSize getImageSize(int resource, Context ctx) {
ImageSize imageSize = new ImageSize();
BitmapFactory.Options dimensions = new BitmapFactory.Options();
dimensions.inJustDecodeBounds = true;
Bitmap mBitmap = BitmapFactory.decodeResource(ctx.getResources(), resource, dimensions);
imageSize.setHeight(dimensions.outHeight);
imageSize.setWidth(dimensions.outWidth);
return imageSize;
}
最好将它们放入:
sellCarLayout.post(new Runnable() {
@Override
public void run() {
//here
});
}
我希望它有所帮助