android的动态图像大小

android的动态图像大小,android,image,imageview,Android,Image,Imageview,我面临着一个问题,从安卓手机到平板电脑都要保持相同的布局 原因是因为我的大多数图像都是固定大小的。因此,从手机切换到平板电脑时会出现问题 我活动的布局是这样的 <relativelayout> - with match_parent for both width and height <linearlayout> - with match_parent for both width and height <linearlayout>

我面临着一个问题,从安卓手机到平板电脑都要保持相同的布局

原因是因为我的大多数图像都是固定大小的。因此,从手机切换到平板电脑时会出现问题

我活动的布局是这样的

<relativelayout> - with match_parent for both width and height
     <linearlayout> - with match_parent for both width and height
          <linearlayout> - with match_parent for for width only and match_content for height
                <imageview> - fixed size - 94 dp and 32 dp
-宽度和高度均与父项匹配
-宽度和高度均为匹配_父项
-仅宽度为匹配_父项,高度为匹配_内容
-固定尺寸-94 dp和32 dp
有没有可能做一些类似于给宽度一个百分比的事情,就像我们在html中做的那样,而不是修复dp?这样它就可以自动调整到父级宽度


谢谢大家!

如果在所有图像中设置相同的宽度

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

请尝试以下操作:

步骤1:使用此代码动态计算设备屏幕的高度和宽度

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

int screenHeight = displaymetrics.heightPixels;
int screenWidth = displaymetrics.widthPixels;
myImageView.getLayoutParams().height = imgHeight;
myImageView.getLayoutParams().width = imgWidth;
步骤2:使用此代码获取活动或片段中的ImageView引用

ImageView myImageView = (ImageView) findViewById(R.id.your_image_view);
第3步:现在根据屏幕的高度和宽度计算ImageView的高度和宽度

假设您想要10%高度的图像视图对应于屏幕; 那么图像高度应该是

int imgHeight = (int) (screenHeight* 0.10); // 10% of screen.
同样的方法,宽度要达到25% 那么应该是:

int imgWidth =  (int) (screenWidth* 0.25); // 25% of screen.
第4步:最后,使用此代码设置ImageView的高度和宽度时出现问题

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

int screenHeight = displaymetrics.heightPixels;
int screenWidth = displaymetrics.widthPixels;
myImageView.getLayoutParams().height = imgHeight;
myImageView.getLayoutParams().width = imgWidth;
希望它能帮助你。让我知道

  • 首先,尝试使您的图像在资源文件夹中具有不同的大小
  • 您还可以使用“匹配父项”和“换行”内容

  • 我认为用不同的尺寸制作相同的图像,并根据它们的尺寸将它们设置为res/drawable(hdpi、mdpi等)。因为android会根据设备dpi自动从这些文件夹中选择图像。

    如果在屏幕宽度中设置3个图像,并使用相同宽度的图像,则使用layout_weight=“1”。并给出布局_width=“0dp”。