简体   繁体   English

不论设备大小如何,都可以将多个Android ImageViews缩放为适合所有屏幕

[英]Scale multiple Android ImageViews to All Fit on Screen Regardless of Device Size

I am new to Android. 我是Android新手。 I have multiple image views in an activity and will be adding more. 我在一个活动中有多个图像视图,并将添加更多图像视图。 I'm using these image views as buttons. 我正在使用这些图像视图作为按钮。 The problem I'm having is getting them to all fit correctly and scale according to the screen size. 我遇到的问题是让它们正确安装并根据屏幕尺寸缩放。 Can I get some help with this? 我可以得到一些帮助吗?

Here is the XML: 这是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/homeBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:scaleType="centerCrop"
        android:src="@drawable/homebackground" />

    <ImageView
        android:id="@+id/publicAffairs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/publicaffairs" />

    <ImageView
        android:id="@+id/actionAlerts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="1dp"
        android:layout_toRightOf="@+id/publicAffairs"
        android:src="@drawable/actionalerts" />

</RelativeLayout>

The two image views I'm using as buttons don't scale according to device size. 我用作按钮的两个图像视图无法根据设备尺寸缩放。

在此处输入图片说明

Yes I have had this problem before. 是的,我以前遇到过这个问题。 Refer to my other answers: 请参阅我的其他答案:

Placing a point at specific position on different screen densities and Resizing images from server 将点放置在不同屏幕密度上的特定位置调整服务器图像的大小

What you need to do is find how much larger this new screen is than your old one, by first getting the dimensions of your device that you are testing on and putting them in for widthOfStandardDevice and heightOfStandardDevice.Once you know how much larger the new screen is then your test one using DisplayMetrics, you make two multipliers to multiply everything by so that the position and size is the same relative to the screen size. 您需要做的就是先获取要测试的设备的尺寸并将其放在widthOfStandardDevice和heightOfStandardDevice上,以查找新屏幕比旧屏幕大多少的时间。然后使用DisplayMetrics进行测试,则使两个乘数乘以所有乘数,以使位置和大小相对于屏幕大小相同。

DisplayMetrics displaymetrics = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

height = displaymetrics.heightPixels;

width = displaymetrics.widthPixels;

float widthMultiplier = width/widthOfStandardDevice;

float heightMultiplier = height/heightOfStandardDevice;

int image_width = (int)(/*regular image width on your test device*/ * widthMultiplier);

int image_height = (int)(/*regular image height on your test device*/ * heightMultiplier);

LayoutParams params = new LayoutParams(image_width, image_height);

params.setMargins(/*regular left margin*/ * widthMultiplier,
    /*regular bottom margin*/ * heightMultiplier,);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);

imageView.setLayoutParams(params);

RelativeLayout x = (RelativeLayout)findViewById(R.id.relative_layout_id);

x.addView(imageView);

also, you should add id's to the ImageViews and RelativeLayout like this: 同样,您应该像这样将id添加到ImageViews和RelativeLayout中:

android:id="@+id/your_id_here"

for the imageView we created in the code earlier, the id would be: 对于我们之前在代码中创建的imageView,其ID为:

android:id="@+id/imageView1"

for the layout it would be: 布局将是:

android:id="@+id/relative_layout_id"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM