我的layout-sw320dp-hdpi文件夹包含以下xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/wall1Layout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/wall4" tools:context=".FireRoomActivity">
<Button android:id="@+id/fishtank" android:layout_width="170dp"
android:layout_height="90dp" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:layout_marginRight="70dp"
android:layout_marginTop="75dp" android:onClick="fillBottle"
android:background="@android:color/transparent" />
</RelativeLayout>
我看过3种类型的屏幕属于sw320dp-hdpi类别:1)samsung galaxy s2(480x800),2)3.7''WVGA(480X800)(这是Nexus One)和3)3.7'' FWVGA(480X854)。
两个问题:
1)galaxy s2和Nexus One之间出现了问题,正如你在下面的截图中看到的那样,按钮“fishtank”在nexus one中稍微偏离。我想这是因为第一个有菜单,主页和后面作为屏幕的一部分,即作为480x800的一部分,而第二个没有。如果我错了,请纠正我,如果两个屏幕尺寸相同,我只是没有看到为什么按钮位于不同位置的原因
2)问题出现在3.7英寸WVGA(480X800)和3.7英寸FWVGA(480X854)之间。正如你可以从屏幕截图中看到的那样,“鱼缸”按钮的位置差异相当大。我该怎么办才能修好它?可能有我不知道的布局技巧
三星galaxy s2(480x800)
Nexus One(3.7英寸WVGA(480X800))
3.7''FWVGA(480X854)
答案 0 :(得分:2)
在这种情况下,我认为使用XML设置合适的按钮大小确实很困难。因为,你知道,很多屏幕尺寸和dpi。每次都会更改背景图像的像素大小...
因此,您可以使用代码解决此问题。 试试以下......
没有尺寸,没有对齐......
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/wall1Layout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/wall4" tools:context=".FireRoomActivity">
<Button
android:id="@+id/fishtank"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/white"
android:onClick="fillBottle" />
/* Background Size */
final double BG_REAL_WIDTH_PX = 510;
final double BG_REAL_HEIGHT_PX = 332;
/* Fish Tank Size */
final double FISH_TANK_REAL_WIDTH_PX = 177;
final double FISH_TANK_REAL_HEIGHT_PX = 90;
/* Start point */
final double FISH_TANK_START_X = 260;
final double FISH_TANK_START_Y = 77;
private void setButtonPosition() {
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager winMan = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
winMan.getDefaultDisplay().getMetrics(displayMetrics);
double dScaleWidth = (double) displayMetrics.widthPixels / BG_REAL_WIDTH_PX;
double dScaleHeight = (double) displayMetrics.heightPixels / BG_REAL_HEIGHT_PX;
double dPosX = dScaleWidth * FISH_TANK_START_X;
double dPosY = dScaleHeight * FISH_TANK_START_Y;
double dTobeFishTankWidth = dScaleWidth * FISH_TANK_REAL_WIDTH_PX;
double dTobeFishTankHeight = dScaleHeight * FISH_TANK_REAL_HEIGHT_PX;
Button btnFishTank = (Button) findViewById(R.id.fishtank);
ViewGroup.LayoutParams params = btnFishTank.getLayoutParams();
params.width = (int) dTobeFishTankWidth;
params.height = (int) dTobeFishTankHeight;
btnFishTank.setLayoutParams(params);
if (Build.VERSION.SDK_INT >= 11) {
btnFishTank.setX((int) dPosX);
btnFishTank.setY((int) dPosY);
}
else {
//TODO: Code for low level OS under 11
}
}
结果是......(在1280x720上)