Android - 无论屏幕尺寸如何,都能保持UI的有序

时间:2014-07-23 21:58:22

标签: android user-interface size screen

我正在制作游戏,而我的UI在我的预览设备中组织得非常好,它会在模拟器设备中混乱,因为屏幕尺寸不同! 我尝试以编程方式实现有组织的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);

        }
    });

但严重的是,上述似乎并不正确,但它不起作用 所以我的问题是如何制作一个与每个屏幕相匹配的UI?

这是特定活动的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>

2 个答案:

答案 0 :(得分:1)

您需要使用xml布局文件夹。您是否需要以编程方式创建布局或者是否可以使用xml文件?您应该创建xml资源并对它们进行充气。

答案 1 :(得分:0)

  1. 您是否阅读了http://developer.android.com/guide/practices/screens_support.html

  2. 您是否考虑使用游戏引擎https://github.com/nicolasgramlich/AndEngine? 它需要在各种设备和许多其他设备上正确渲染。