所以我在这里努力创建一个图标的2D地图,其中每个图标是48x48像素,地图是9x9网格(因此,大小为432x432像素)。我尝试了,但没有成功,从GridView开始,后来决定尝试在LinearView中使用AbsoluteView。这是XML文件的开头(我现在还没有添加所有81个图标只是为了对这个问题保持简洁):
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="442px"
android:layout_height="442px"
android:padding="5px"
android:layout_gravity="center_horizontal"
android:background="#008000"
>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="0px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="48px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="96px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="144px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="192px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="240px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="288px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="336px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="384px"
android:layout_y="0px"
/>
<ImageView
android:background="@drawable/water2"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="0px"
android:layout_y="48px"
/>
</AbsoluteLayout>
当我在Eclipse中尝试这个时,它看起来非常完美。当我把它加载到我的三星Captivate上时,这是错的。就像图标被拉伸一样,即使我已经指定了精确的像素尺寸。
我确定我可能会犯一些菜鸟错误,但无论我尝试什么,我似乎无法使图标/抽屉显示在他们的确切尺寸上。如果有人能指出我做错了什么,我会非常感激。
感谢。
所以我更改了XML文件以尝试使用TableLayout,如下所示:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<TableRow>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0px">
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:src="@drawable/water2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</TableRow>
</TableLayout>
即使我指定了ImageViews的确切尺寸,它们仍然会被拉伸,只显示大约5个左右的图标。
答案 0 :(得分:1)
AbsoluteLayout被删除。使用AbsoluteLayout有很多更好的方法来实现这一点。
如果您不关心每行中有多少项,GridView将运行良好。这是处理多种屏幕尺寸的简单方法。
如果您每行需要9个,则可以使用TableLayout。有关详细介绍,请参阅TableLayout tutorial。
基本上我会创建一个包含9行的TableLayout。每行包含水平LinearLayout和9个ImageViews。
要防止图片拉伸,请务必将scaleType设置为fitXY,将ImageViews和LinearLayouts的layout_width
和layout_height
设置为WRAP_CONTENT
。
您还需要记住手机上可用的许多不同屏幕密度。一个48x48像素的图像在一个密度上可能正常工作,但在具有大屏幕尺寸的高密度手机上可能看起来不太好。有关概述,请参阅supporting multiple screen sizes。基本上,您需要为不同的屏幕密度提供不同的图像资源。
答案 1 :(得分:0)
我不确定我是否正确,但当我阅读你的问题时,我想到了两个想法。
您可能尝试过第一个..如果图标已经具有您想要的尺寸,请不要将layout_width和height添加到XML(无论如何,它确实没有意义,因为图标非常适合您的ImageView)。 / p>
第二个是关于像素的单位。我最近读过,使用px作为度量单位不是Android上的推荐做法。而不是px,使用dp(密度像素,我认为)用于大小,使用sp(缩放像素)用于字体大小。也许像素尺寸在手机上无法正常工作:P
希望这可以帮到你:D