如何适合我的布局,在Android的任何屏幕 [英] How to fit my Layout to any Screen in Android

查看:130
本文介绍了如何适合我的布局,在Android的任何屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立我自己的应用程序游戏为Android,这是一个井字游戏。
我的活动主要包含的 ImageViews 的,都放置在DIR 清晰度影像\\绘-MDPI ONLY。
可绘制文件夹的其余部分都是空的。

I'm building my own application game for Android, it's a Tic Tac Toe game. my activities contains mostly ImageViews , all the images placed in the dir res\drawable-mdpi ONLY. the rest of the drawable folders are empty.

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

my problem is that when i run the app on a 5.4 inch MDPI screen it works fine and it looks like this:

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

but when i run the app on another screen of different size for example this 4.65 inch XHDPI screen, it looks like this:

在上述活动,我用的的LinearLayout 的和的 RelativeLayout的的,我应该怎么做才能适合我的布局,任何屏幕?

in the above activity i used LinearLayout and RelativeLayout, what should i do to fit my layout to any screen ?

感谢


  • 感谢编辑此图片

这是activity_board.xml的XML文件的code:

this is the code of the xml file of activity_board.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>

推荐答案

创建一个绘制文件夹,只需单词绘制,RES ,把你的游戏图像在那里。这将使得游戏的图像跨越每一个器件密度相同的。

Create a drawable folder, just the word "drawable", in res and put your game images in there. That will make the game images the same across every device density.

不看你的XML,我会同意,你可能使用硬codeD DP 值,而不是大小的数学布局,即重量, match_parent alignParentRight

Without looking at your XML, I would agree that you're probably using hardcoded dp values instead of sizing the layout with math, ie, weights, match_parent, alignParentRight, etc.

我已经调整你的XML更容易。我没有把它插入一个previewer或任何东西。

I have adjusted your XML to be easier. I did not plug it into a previewer or anything.

<?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>

这篇关于如何适合我的布局,在Android的任何屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆