这是我的问题: 我在图像视图中放置了分辨率为200x200的图像,高度和宽度均为200dip,但显示的图像似乎大于200x200,这是我发现问题的方法。
我使用下面的代码来获取屏幕的宽度和高度。并且发现我的屏幕是540x800,但图像宽度大于我屏幕的一半。
WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
有没有人可以告诉我原因。下面是xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation = "vertical"
android:background = "@drawable/mainbg2"
>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/layout_menu_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ImageView
android:id="@+id/img_menu_normal"
android:layout_width="200dip"
android:layout_height="200dip"
android:src="@drawable/menu_normal"/>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
与密度无关的像素(dp) 在定义UI布局时应使用的虚拟像素单元,以与密度无关的方式表达布局尺寸或位置。
与密度无关的像素相当于160 dpi屏幕上的一个物理像素,这是由此假定的基线密度 用于“中等”密度屏幕的系统。在运行时,系统 根据需要透明地处理dp单元的任何缩放 关于使用中的屏幕的实际密度。 dp单位的转换 屏幕像素很简单:px = dp *(dpi / 160)。例如,在a 240 dpi屏幕,1 dp等于1.5物理像素。你应该经常使用 定义应用程序UI时的dp单位,以确保正确显示 你的用户界面在不同密度的屏幕上。
所以我假设您的设备不是mdpi,它使用更大的比例。如果您想在设备上制作精确的像素大小,可以使用200px
。但首先请查看上面的文档链接。
答案 1 :(得分:0)
它显示200X200 dp大小的图像在视图中您可以使用此文件中的xmp更改进行检查
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layout_menu_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_menu_normal"
android:layout_width="200dip"
android:layout_height="200dip"
android:background="@android:color/black"
android:src="@drawable/ic_launcher" />
</LinearLayout>
我希望你能理解那里发生的事情。我在相对视图的中心设置图像,并将imageview的背景颜色更改为黑色,将主视图bg颜色更改为white.it的工作代码。