2014-07-23 42 views
0

我正在制作游戏,虽然我的UI在我的预览设备中组织得非常好,但它在仿真器设备中变得混乱,这是因为屏幕大小不同! 我试图实现有一个有组织的UI程序,这意味着我首先测量屏幕尺寸,并根据这是多么大或小我创造的这个大小的按钮,图片,textviews ...Android - 保持UI无论屏幕大小如何组织

btn2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      btn_params=new RelativeLayout.LayoutParams(40,40); 
      btn_params.rightMargin=0; 
      btn_params.bottomMargin=0; 
      btn1.setLayoutParams(btn_params); 

      waterlily_params=new RelativeLayout.LayoutParams(layout.getHeight()/3,layout.getHeight()/3); 
      waterlily_params.leftMargin=layout.getWidth()/2; 
      waterlily_params.bottomMargin=0; 
      iv2.setLayoutParams(waterlily_params); 

      frog_params=new RelativeLayout.LayoutParams(iv2.getHeight()/2,iv2.getHeight()/2); 
      frog_params.leftMargin=layout.getWidth()/2; 
      frog_params.bottomMargin=0; 
      iv1.setLayoutParams(frog_params); 

      iv2.setVisibility(view.VISIBLE); 
      iv1.setVisibility(view.VISIBLE); 
      btn2.setVisibility(view.GONE); 
      btn1.setVisibility(view.VISIBLE); 

     } 
    }); 

但严重上述doesnt似乎并没有工作 所以我的问题是如何使一个用户界面,将匹配每个屏幕?

这里是特定活动的XML:

im using xml files ! Here is the xml file of the specific activity : 



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.example.user.catchthefly.MainGame" 
android:clickable="true" 
android:id="@+id/MyLayout" 
android:focusableInTouchMode="true"> 


<ImageView 
    android:layout_width="300dp" 
    android:layout_height="200dp" 
    android:id="@+id/waterlily" 
    android:background="@drawable/water_lily" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:visibility="invisible" /> 

<ImageView 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    android:id="@+id/frog" 
    android:background="@drawable/f132" 
    android:layout_gravity="bottom" 
    android:layout_alignBottom="@+id/waterlily" 
    android:layout_centerHorizontal="true" 
    android:visibility="invisible" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="Tap Here To Start Your Game" 
    android:id="@+id/StartingButton" 
    android:enabled="true" 
    android:textSize="30dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:background="#00000000" /> 

<Button 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:id="@+id/MovingObj" 
    android:background="#ffff00e1" 
    android:layout_above="@+id/StartingButton" 
    android:layout_toLeftOf="@+id/frog" 
    android:layout_toStartOf="@+id/frog" 
    android:visibility="invisible" /> 
</RelativeLayout> 
+0

[支持多屏幕](http://developer.android.com/guide/practices/screens_support.html) –

回答

1

你需要使用XML布局文件夹。你需要以编程方式创建布局,还是可以使用xml文件?您应该创建xml资源并对它们进行膨胀。

+0

我可以使用xml文件。我更新了具体活动的xml文件的线程,我发布的代码来自!谢谢你的回答:) – kasf

+0

你也有另一种选择。也许你不想使用不同的布局,但在不同的屏幕尺寸相同的布局不同的尺寸 – AlexBalo

+0

看看这个链接。逻辑和使用不同的布局是一样的,但在这种情况下,你只有1个布局资源,你会给不同的维度。 http://developer.android.com/guide/topics/resources/more-resources.html – AlexBalo