320dp 宽图像占据 Android 模拟器的整个屏幕
我有一个 320dp
宽的横幅,但它填充了 480x800
android 模拟器的整个宽度。
这是横幅的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/add_banner"
android:layout_width="320dip"
android:layout_height="50dip"
>
<ImageView android:id="@+id/add_image"
android:layout_width="320dip"
android:layout_height="50dip"
android:src="@drawable/your_ad_here"
/>
<Button android:id="@+id/btn_addclose"
android:layout_width="25dip"
android:layout_height="25dip"
android:gravity="center"
android:layout_alignTop="@id/add_image"
android:layout_alignRight="@id/add_image"
android:text="x"
/>
</RelativeLayout>
我已将其包含在我的 main.xml 文件中:
<include android:id="@+id/add_banner"
layout="@layout/tlp_add_banner"
android:layout_width="320dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
为了确保我的模拟器是 480x800
,我什至记录了运行时的指标。它是480x800
。那么为什么横幅占据了整个宽度呢?
I have a banner which is 320dp
wide but it is filling the entire width of a 480x800
android emulator.
Here is the XML for the banner:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/add_banner"
android:layout_width="320dip"
android:layout_height="50dip"
>
<ImageView android:id="@+id/add_image"
android:layout_width="320dip"
android:layout_height="50dip"
android:src="@drawable/your_ad_here"
/>
<Button android:id="@+id/btn_addclose"
android:layout_width="25dip"
android:layout_height="25dip"
android:gravity="center"
android:layout_alignTop="@id/add_image"
android:layout_alignRight="@id/add_image"
android:text="x"
/>
</RelativeLayout>
I have included it in my main.xml file as :
<include android:id="@+id/add_banner"
layout="@layout/tlp_add_banner"
android:layout_width="320dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
To make sure that my emulator is 480x800
, I even logged the metrics on runtime. It is 480x800
. Then why is the banner taking up the entire width?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
dip
表示与密度无关的像素。您的 480x800 模拟器很可能是 HDPI(高密度,或每英寸高点数),这意味着 320dp 将转换为 320*1.5 实际像素(即 480)。只有在比例因子为 1 的 MDPI(中密度)屏幕上,您才能真正获得 320 像素。您可以像这样获取当前屏幕的比例因子:
如果您确实希望其宽度为 320 像素,无论屏幕密度如何(根本不建议这样做),您可以指定:
dip
means density-independent pixels. Your 480x800 emulator is most probably HDPI (high density, or high dots per inch), which means that 320dp will translate to 320*1.5 actual pixels (which is 480). Only on an MDPI (medium density) screen, where the scale factor is 1 will you actually get 320 pixels.You can get the scale factor for the current screen like this:
If you really want it to be 320 pixels wide, regardless of the screen density (this is not recommended at all), you can just specify:
倾角不是物理像素,而是取决于显示密度的虚构数字。更多信息请参见:
http://developer.android.com/guide/practices/ screen_support.html
dip aren't physical pixels but a made up number that depends on the display density. More info here:
http://developer.android.com/guide/practices/screens_support.html