无法在Nexus One上获得精确的布局

时间:2010-04-27 10:09:25

标签: android

我想在Nexus One上使用精确布局,我的代码是这样的:

<?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>

但事实证明,Nexus One的屏幕宽度不是480像素。因此,此LinearLayout将超出屏幕宽度。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

有几件事

1)在设计android GUI时不建议使用px

检查available resourcesthis question以了解px,dp和sp之间的差异。

2)如果您使用的是drawables,我认为它是图像,请使用

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

而不是尝试将容器(ImageView)调整为内容(图像)。 Android将负责其余部分。了解何时使用wrap_content或fill_parent是GUI设计的关键。

3)Nexus One宽480像素。问题在于您的布局设计,只需继续工作,您将学习使Android设计中的UI设计比大多数其他平台更容易的技巧。

此外,我认为此代码段包含在View中,您尚未声明命名空间。也许你在布局的其余部分出了问题。

我希望这会有所帮助。