开发者

320dp wide image taking up entire screen of Android emulator

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="开发者_StackOverflow中文版@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?


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:

float scale = getResuorces().getDisplayMetrics().density;

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:

android:layout_width="320px"


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜