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

Java 如何使我的布局适合Android中的任何屏幕,java,android,eclipse,layout,Java,Android,Eclipse,Layout,我正在为Android开发自己的应用程序游戏,这是一款井字游戏。 “我的活动”主要包含图像视图,所有图像仅放置在dirres\drawable mdpi中。 其余可绘制文件夹为空 我的问题是,当我在5.4英寸MDPI屏幕上运行应用程序时,它工作正常,如下所示: 但当我在另一个大小不同的屏幕上运行应用程序时,例如这个4.65英寸XHDPI屏幕,它看起来是这样的: 在上面的活动中,我使用了LinearLayout和RelativeLayout,我应该如何使我的布局适合任何屏幕 谢谢 谢谢你编辑

我正在为Android开发自己的应用程序游戏,这是一款井字游戏。 “我的活动”主要包含图像视图,所有图像仅放置在dirres\drawable mdpi中。 其余可绘制文件夹为空

我的问题是,当我在5.4英寸MDPI屏幕上运行应用程序时,它工作正常,如下所示:

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

在上面的活动中,我使用了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>


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

不看XML,我同意您可能使用了硬编码的
dp
值,而不是使用数学来调整布局大小,即权重、
match\u parent
alignParentRight
,等等

我已将您的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>

您是否使用RelativeLayout

你应该 1.使用RelativeLayout作为基本容器 2.将“玩家”和“系统”分数放在同一水平线布局内,以便它们不会重叠 3.将“游戏结束”按钮作为一个元素,与父项底部对齐


您附加的屏幕截图显示,这是由软状态栏引起的问题。RelativeLayout会有帮助,您应该尽量避免在数字中硬编码尺寸,但使用边距、“layout_toLeftOf”、“layout_toRightOf”等在RelativeLayout中进行自动调整。

我认为您使用的是使用
dp
等硬编码宽度和高度值。。开始使用诸如
wrap\u content
fill\u parent
等值。在线性布局的情况下使用权重。这些值会根据不同的屏幕调整大小。感谢您的回复,我添加了活动的xml文件,请查看此链接:-我想这就是我所做的,请查看我添加的xml代码。谢谢:)例如,block1、block2和block3是不相关的,但实际上block2应该是block1的右侧。我会像你说的那样尝试创建可绘制文件夹,但也请看一下我添加的代码,它们都使用了
包装内容
匹配父项
谢谢我的编辑。我清除了很多不必要的垃圾。它包括三个主要部分:记分板、游戏板以及游戏和倒计时区。这可能不太合适,但肯定更好。该设计有点缺陷,因为背景图像是大多数图形,因此背景将在不同的纵横比上拉伸。我建议你把背景分成几部分。我创建了文件夹
drawable-xxxhdpi
drawable-xxhdpi
drawable-xhdpi
,以及其他,我使用了一个名为
9Patch-resizer
的软件来更改我的
mdpi(默认图片为xxxhdpi)
,红色的
按钮的大小调整得很好,但是在board活动中,board(这是一张图像)的大小调整得很好,但当我在我的
xxxhdpi
手机上运行时,它仍然会拉伸,就像图中一样。。。我现在正在尝试你的代码,我不知道你在第一个线性布局中所说的调整我是什么意思,但我会尝试删除这两个属性..调整我意味着你必须输入自己的数字并使它们看起来漂亮。Xxxhdpi目前是一种非常不受欢迎的分辨率,很容易被忽略。它仍然不起作用,我无法控制此特定活动中的图像和字体,而且您更改的xml代码仍然不起作用