我使用與設置的Nexus 5的設備我得到了所有的圖像在我的應用程序的Android模擬器排隊很好佈局位置不正確縮放爲不同的設備
設計我的應用程序然而,當我然後嘗試使用設置爲其他設備的仿真器進行測試時:Nexus 6,7和9,所有x和y位置都沒有相對於背景圖像正確縮放。
這裏是我的XML代碼:
<FrameLayout
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spriteLayout">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25dp"
android:minHeight="25dp"
android:layout_gravity="center_vertical"
android:id="@+id/AustraliaLayout" />
<RelativeLayout
android:minWidth="40px"
android:minHeight="40px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Fishes1Layout" />
<RelativeLayout
android:minWidth="20px"
android:minHeight="20px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Fishes3Layout" />
<RelativeLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Fishes4Layout" />
<RelativeLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Fishes5Layout" />
<RelativeLayout
android:minWidth="40px"
android:minHeight="40px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Fishes6Layout" />
<RelativeLayout
android:minWidth="15px"
android:minHeight="15px"
android:layout_width="15dp"
android:layout_height="15dp"
android:id="@+id/Large_Z_Layout" />
<RelativeLayout
android:minWidth="12px"
android:minHeight="12px"
android:layout_width="10dp"
android:layout_height="10dp"
android:id="@+id/Medium_Z_Layout" />
<RelativeLayout
android:minWidth="6px"
android:minHeight="6px"
android:layout_width="6dp"
android:layout_height="6dp"
android:id="@+id/Small_Z_Layout" />
<RelativeLayout
android:minWidth="35px"
android:minHeight="35px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/WombatLayout1" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ToadLayout1">
<RelativeLayout
android:minWidth="40px"
android:minHeight="40px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:id="@+id/ToadLayout2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/KangarooLayout1">
<RelativeLayout
android:minWidth="40px"
android:minHeight="40px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:id="@+id/KangarooLayout2" />
</RelativeLayout>
<RelativeLayout
android:minWidth="35px"
android:minHeight="35px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SnakeLayout1">
<RelativeLayout
android:minWidth="50px"
android:minHeight="50px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/SnakeLayout2" />
</RelativeLayout>
<RelativeLayout
android:minWidth="40px"
android:minHeight="40px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BirdLayout" />
<RelativeLayout
android:minWidth="30px"
android:minHeight="30px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Marker1Layout" />
<RelativeLayout
android:minWidth="30px"
android:minHeight="30px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Marker2Layout" />
<RelativeLayout
android:minWidth="30px"
android:minHeight="30px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Marker3Layout" />
<RelativeLayout
android:minWidth="30px"
android:minHeight="30px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Marker4Layout" />
<RelativeLayout
android:minWidth="30px"
android:minHeight="30px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/CarMarkerLayout" />
</FrameLayout>
這裏是我處理的佈局編程:
Australia = new ImageView(this);
Australia.SetImageResource(Resource.Drawable.australia);
Australia.Visibility = ViewStates.Visible;
AustraliaLayout = FindViewById<RelativeLayout>(Resource.Id.AustraliaLayout);
AustraliaLayout.AddView(Australia);
Fishes1 = new ImageView(this);
Fishes1.SetImageResource(Resource.Drawable.fishes1_sprite);
Fishes1.Visibility = ViewStates.Visible;
Fishes1Layout = FindViewById<RelativeLayout>(Resource.Id.Fishes1Layout);
Fishes1Layout.AddView(Fishes1);
Fishes1Layout.SetX(DP_to_Pixel(133.33));
Fishes1Layout.SetY(DP_to_Pixel(323.5));
Fishes2 = new ImageView(this);
Fishes2.SetImageResource(Resource.Drawable.fishes2_sprite);
Fishes2.Visibility = ViewStates.Visible;
Fishes2Layout = FindViewById<RelativeLayout>(Resource.Id.Fishes2Layout);
Fishes2Layout.AddView(Fishes2);
Fishes2Layout.SetX(DP_to_Pixel(50));
Fishes2Layout.SetY(DP_to_Pixel(350));
Fishes3 = new ImageView(this);
Fishes3.SetImageResource(Resource.Drawable.fishes3_sprite);
Fishes3.Visibility = ViewStates.Visible;
Fishes3Layout = FindViewById<RelativeLayout>(Resource.Id.Fishes3Layout);
Fishes3Layout.AddView(Fishes3);
Fishes3Layout.SetX(DP_to_Pixel(303.33));
Fishes3Layout.SetY(DP_to_Pixel(170));
Fishes4 = new ImageView(this);
Fishes4.SetImageResource(Resource.Drawable.fishes4_sprite);
Fishes4.Visibility = ViewStates.Visible;
Fishes4Layout = FindViewById<RelativeLayout>(Resource.Id.Fishes4Layout);
Fishes4Layout.AddView(Fishes4);
Fishes4Layout.SetX(DP_to_Pixel(100));
Fishes4Layout.SetY(DP_to_Pixel(116.67));
Fishes5 = new ImageView(this);
Fishes5.SetImageResource(Resource.Drawable.fishes4_sprite);
Fishes5.Visibility = ViewStates.Visible;
Fishes5Layout = FindViewById<RelativeLayout>(Resource.Id.Fishes5Layout);
Fishes5Layout.AddView(Fishes5);
Fishes5Layout.SetX(DP_to_Pixel(303.33));
Fishes5Layout.SetY(DP_to_Pixel(360));
Fishes6 = new ImageView(this);
Fishes6.SetImageResource(Resource.Drawable.fishes1_sprite);
Fishes6.Visibility = ViewStates.Visible;
Fishes6Layout = FindViewById<RelativeLayout>(Resource.Id.Fishes6Layout);
Fishes6Layout.AddView(Fishes6);
Fishes6Layout.SetX(DP_to_Pixel(26.67));
Fishes6Layout.SetY(DP_to_Pixel(173.33));
Snake1 = new ImageView(this);
Snake1.SetImageResource(Resource.Drawable.snake_sprite);
Snake1.Visibility = ViewStates.Visible;
SnakeLayout1 = FindViewById<RelativeLayout>(Resource.Id.SnakeLayout1);
SnakeLayout2 = FindViewById<RelativeLayout>(Resource.Id.SnakeLayout2);
SnakeLayout2.AddView(Snake1);
SnakeLayout1.SetX(DP_to_Pixel(120));
SnakeLayout1.SetY(DP_to_Pixel(253.33));
ZLarge = new ImageView(this);
ZLarge.SetImageResource(Resource.Drawable.z);
ZLarge.Visibility = ViewStates.Visible;
ZLargeLayout = FindViewById<RelativeLayout>(Resource.Id.Large_Z_Layout);
ZLargeLayout.AddView(ZLarge);
ZLargeLayout.SetX(DP_to_Pixel(258.33));
ZLargeLayout.SetY(DP_to_Pixel(290.33));
// move down by 7
ZMedium = new ImageView(this);
ZMedium.SetImageResource(Resource.Drawable.z);
ZMedium.Visibility = ViewStates.Visible;
ZMediumLayout = FindViewById<RelativeLayout>(Resource.Id.Medium_Z_Layout);
ZMediumLayout.AddView(ZMedium);
ZMediumLayout.SetX(DP_to_Pixel(268.33));
ZMediumLayout.SetY(DP_to_Pixel(307));
ZSmall = new ImageView(this);
ZSmall.SetImageResource(Resource.Drawable.z);
ZSmall.Visibility = ViewStates.Visible;
ZSmallLayout = FindViewById<RelativeLayout>(Resource.Id.Small_Z_Layout);
ZSmallLayout.AddView(ZSmall);
ZSmallLayout.SetX(DP_to_Pixel(264.33));
ZSmallLayout.SetY(DP_to_Pixel(318));
Wombat1 = new ImageView(this);
Wombat1.SetImageResource(Resource.Drawable.wombat_sprite);
Wombat1.Visibility = ViewStates.Visible;
WombatLayout1 = FindViewById<RelativeLayout>(Resource.Id.WombatLayout1);
WombatLayout1.AddView(Wombat1);
WombatLayout1.SetX(DP_to_Pixel(236.84));
WombatLayout1.SetY(DP_to_Pixel(323.338));
Toad1 = new ImageView(this);
Toad1.SetImageResource(Resource.Drawable.toad_sprite);
Toad1.Visibility = ViewStates.Visible;
ToadLayout1 = FindViewById<RelativeLayout>(Resource.Id.ToadLayout1);
ToadLayout2 = FindViewById<RelativeLayout>(Resource.Id.ToadLayout2);
ToadLayout2.AddView(Toad1);
ToadLayout1.SetX(DP_to_Pixel(261.67));
ToadLayout1.SetY(DP_to_Pixel(210));
ToadLayout2.SetY(DP_to_Pixel(13.33));
Kangaroo1 = new ImageView(this);
Kangaroo1.SetImageResource(Resource.Drawable.kangaroo_sprite);
Kangaroo1.Visibility = ViewStates.Visible;
KangarooLayout1 = FindViewById<RelativeLayout>(Resource.Id.KangarooLayout1);
KangarooLayout2 = FindViewById<RelativeLayout>(Resource.Id.KangarooLayout2);
KangarooLayout2.AddView(Kangaroo1);
KangarooLayout1.SetX(DP_to_Pixel(30));
KangarooLayout1.SetY(DP_to_Pixel(223.33));
KangarooLayout2.SetY(DP_to_Pixel(13.33));
Bird = new ImageView(this);
Bird.SetImageResource(Resource.Drawable.bird_sprite);
Bird.Visibility = ViewStates.Visible;
BirdLayout = FindViewById<RelativeLayout>(Resource.Id.BirdLayout);
BirdLayout.AddView(Bird);
BirdLayout.SetX(DP_to_Pixel(150));
BirdLayout.SetY(DP_to_Pixel(170));
最後,我創建了一個方法來轉換的數量像素到DP,我認爲可以解決這個問題,但它仍然不能正常工作:
private int DP_to_Pixel(double dp)
{
return (int) (dp * Resources.DisplayMetrics.Density);
}
我對編程非常陌生,我嘗試了所有我能想到的方法來糾正問題。任何援助與這個問題將不勝感激!
使用DP代替PX –
感謝莫哈末,但正如我所說在我的帖子底部,我確實將像素轉換爲dp –
您正在將dp轉換爲px,請將px轉換爲dp我擁有我在回答框中提到了該方法。 –