tags:

views:

215

answers:

1

I want to use precise layout on Nexus One, my code is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="90dp">

    <ImageView
        android:layout_width="5dp"
        android:layout_height="fill_parent"
        android:src="@drawable/d10" />

    <ImageView
        android:layout_width="94dp"
        android:layout_height="fill_parent"
        android:src="@drawable/d5" />

    <ImageView
        android:layout_width="94dp"
        android:layout_height="fill_parent"
        android:src="@drawable/d6" />

    <ImageView
        android:layout_width="94dp"
        android:layout_height="fill_parent"
        android:src="@drawable/d7" />

    <ImageView
        android:layout_width="94dp"
        android:layout_height="fill_parent"
        android:src="@drawable/d8" />

    <ImageView
        android:layout_width="94dp"
        android:layout_height="fill_parent"
        android:src="@drawable/d9" />

    <ImageView
        android:layout_width="5dp"
        android:layout_height="fill_parent"
        android:src="@drawable/d10" />

</LinearLayout>

</LinearLayout>

But it turns out on Nexus One, the screen width is not 480 px. So this LinearLayout will exceed the screen width.

How should I fix this?

+1  A: 

Several things

1) It's not recommended to use px when designing android GUI

check available resources or this question to learn the differences between px, dp and sp.

2) If you are using drawables, which I presume are images, use

android:layout_width="wrap_content"
android:layout_height="wrap_content"

instead of trying to adjust the container (ImageView) to the content (the image). Android will take care of the rest. Learning when to use wrap_content or fill_parent is key to GUI design.

3) Nexus One IS 480 px wide. The problem lays in your layout design, just keep on working and you'll learn the tricks that make UI design much easier in Android than most of other platforms.

Also, I asume this code snippet is contained in a View, you've not declared the namespace. Maybe you have something wrong in the rest of the layout.

I hope this helps.

Maragues
1) yes, i admit px is not a good way. In fact I've tried dp and sp, but they don't work as well. I'm trying to use the most specific way to see where is the problwm.2) okay.. I've tried "wrap_content" as well, but it doesn't work. IMHO, since my drawable are the same size as I specified, this makes no difference with "wrap_content"3) Yes I do have declared the namespace, now I've uploaded all the code, hope this explains.
Johnny
1) The thing is a dp varies in size depending on the resolution you are working on, hence 94dp != 94px if the resolution is higher or lower than 160 (HTC Hero). Nexus one is 240.2) Did you try wrap_content both for width and height? (also in the LinearLayout's height containing the buttons) I use android:background instead of src in ImageViews, I can't say if there's any difference, but that's what I use.I don't see any problem in your code, I must be missing something. If you could upload a pic maybe we could figure out something more.
Maragues
Thanks for the comments Maragues. This time I've used all wrap_content for LinearLayout and ImageViews, and it works. I don't know why, but I must not understand the android-layout-desity system..
Johnny
And whether I used android:src or android:background, they all works. So I think the magic here is the wrap_content. Though I still don't understand why dp/sp/px doesn't work.
Johnny
Think I found something. This time I use px, but add android:scaleType="fitXY". And it works as well. I think it might be the default scale type problem.
Johnny
Great, I'm glad I could help. Anyway, my advice is to stick with wrap_content whenever you can. Good luck!
Maragues
thanks, but using wrap_content means dependancies on images, which makes the apk size bigger if we supports more screens.
Johnny