如何使我的布局适合Android中的任何屏幕

时间:2014-12-09 02:58:19

标签: java android eclipse layout

我正在为Android构建自己的应用程序游戏,它是一款Tic Tac Toe游戏。 我的活动主要包含 ImageViews ,所有图片只放置在目录 res \ drawable-mdpi 中。 其余的可绘制文件夹都是空的。

我的问题是,当我在 5.4英寸 MDPI屏幕上运行应用程序时,它工作正常,看起来像这样:

http://i.stack.imgur.com/AK3jo.jpg

但是当我在不同尺寸的另一个屏幕上运行应用程序时,例如这个 4.65英寸 XHDPI屏幕,它看起来像这样:

enter image description here

在上面的活动中,我使用了 LinearLayout RelativeLayout ,我应该如何使我的布局适合任何屏幕?

感谢

  • 感谢编辑图片

这是activity_board.xml的xml文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/board_page_bg"
android:gravity="center" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="@drawable/board"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/players_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="2dp"
                    android:text="@string/playersName"
                    android:textSize="30sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/players_score"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="150dp"
                    android:layout_marginTop="5dp"
                    android:text="@string/playersPoints"
                    android:textSize="25sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right" >

                <TextView
                    android:id="@+id/machines_score"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="172dp"
                    android:layout_marginTop="5dp"
                    android:text="@string/machinePoints"
                    android:textSize="25sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right" >

                <TextView
                    android:id="@+id/machines_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="50dp"
                    android:onClick="openChat"
                    android:clickable="true"
                    android:text="@string/machineName"
                    android:textSize="30sp" />
            </RelativeLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" >

            <ImageView
                android:id="@+id/block1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:onClick="touch"
                android:src="@drawable/block_1_empty" />

            <ImageView
                android:id="@+id/block2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="155dp"
                android:onClick="touch"
                android:src="@drawable/block_2_empty" />

            <ImageView
                android:id="@+id/block3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="310dp"
                android:onClick="touch"
                android:src="@drawable/block_3_empty" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp" >

            <ImageView
                android:id="@+id/block4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:onClick="touch"
                android:src="@drawable/block_4_empty" />

            <ImageView
                android:id="@+id/block5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="150dp"
                android:layout_marginTop="10dp"
                android:onClick="touch"
                android:src="@drawable/block_5_empty" />

            <ImageView
                android:id="@+id/block6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="310dp"
                android:layout_marginTop="10dp"
                android:onClick="touch"
                android:src="@drawable/block_6_empty" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="55dp" >

            <ImageView
                android:id="@+id/block7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:onClick="touch"
                android:src="@drawable/block_7_empty" />

            <ImageView
                android:id="@+id/block8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="155dp"
                android:onClick="touch"
                android:src="@drawable/block_8_empty" />

            <ImageView
                android:id="@+id/block9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="315dp"
                android:onClick="touch"
                android:src="@drawable/block_9_empty" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="6dp" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/game_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="105dp"
                    android:text="@string/gameNumber"
                    android:textSize="25sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/count_down"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="390dp"
                    android:text="@string/countDown"
                    android:textSize="25sp" />
            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/end_game"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/EndGame"
            android:onClick="endGame"
            android:src="@drawable/endgame_btn_1" />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

drawable中创建一个res文件夹,只需“drawable”一词,然后将游戏图片放入其中。这将使每个设备密度的游戏图像相同。

在不查看XML的情况下,我同意您可能正在使用硬编码的dp值而不是使用数学调整布局,即权重,match_parentalignParentRight

我已将您的XML调整为更容易。我没有把它插入预览版或任何东西。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/board_page_bg"
android:gravity="center" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="ADJUST ME"
        android:layout_marginRight="ADJUST ME"
        android:background="@drawable/board"
        android:orientation="vertical" >

        <!-- Scoreboard -->
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp" >

            <TextView
                android:id="@+id/players_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="2dp"
                android:layout_alignParentLeft="true"
                android:text="@string/playersName"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/players_score"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="150dp"
                android:layout_marginTop="5dp"
                android:layout_toLeftOf="@id/players_name"
                android:text="@string/playersPoints"
                android:textSize="25sp" />

            <TextView
                android:id="@+id/machines_score"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="172dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/machines_name"
                android:text="@string/machinePoints"
                android:textSize="25sp" />

            <TextView
                android:id="@+id/machines_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="50dp"
                android:onClick="openChat"
                android:layout_alignParentRight="true"
                android:clickable="true"
                android:text="@string/machineName"
                android:textSize="30sp" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/block1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_1_empty" />

            <ImageView
                android:id="@+id/block2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_2_empty" />

            <ImageView
                android:id="@+id/block3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_3_empty" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/block4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_4_empty" />

            <ImageView
                android:id="@+id/block5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_5_empty" />

            <ImageView
                android:id="@+id/block6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_6_empty" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/block7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_7_empty" />

            <ImageView
                android:id="@+id/block8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_8_empty" />

            <ImageView
                android:id="@+id/block9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="touch"
                android:src="@drawable/block_9_empty" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="ADJUST ME" >

            <TextView
                android:id="@+id/game_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="@string/gameNumber"
                android:textSize="25sp" />

            <TextView
                android:id="@+id/count_down"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="@string/countDown"
                android:textSize="25sp" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/end_game"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/EndGame"
            android:onClick="endGame"
            android:src="@drawable/endgame_btn_1" />
    </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

您使用的是RelativeLayout吗?

你应该 1.使用RelativeLayout作为基本容器 2.把#34;播放器&#34;和&#34;系统&#34;在相同的水平LinearLayout内得分,以便它们不会重叠 3.放置&#34;结束游戏&#34;按钮作为对齐父底部的元素

您附带的屏幕截图显示,这是由软状态栏引起的问题。 RelativeLayout会有所帮助,你应该尝试防止对数字中的维度进行硬编码,但是使用边距在RelativeLayout中进行自动调整,&#34; layout_toLeftOf&#34;,&#34; layout_toRightOf&#34;等等。