我使用设计器设置为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 );
}
我对编程非常陌生,而且我已经尝试了所有我能想到的解决问题的方法。对此问题的任何帮助将不胜感激!
答案 0 :(得分:1)
DP instead of PX
。ConstraintLayout负责管理定位和 可视组件的大小调整行为(也称为小部件) 它包含。
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
// YOUR REST WORK
</android.support.constraint.ConstraintLayout>
致电
compile 'com.android.support.constraint:constraint-layout:1.0.2'
答案 1 :(得分:0)
要将px转换为dp,请使用此....
public static int Pixel_To_DP(int pixel) {
return (int) (pixel/ Resources.getSystem().getDisplayMetrics().density);
}