简体   繁体   English

如何使布局在不同的屏幕尺寸上缩放?

[英]How to make my layout scale on different screen sizes?

I have simple layout with 4 different levels/modes you can play. 我的布局简单,可以玩4种不同的关卡/模式。 Problem is when i preview layout on different screen sizes it doesn't appear same: Image 问题是当我在不同屏幕尺寸上预览布局时,它看起来不一样: 图片

Here is layout code: 这是布局代码:

    <?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="match_parent"
    android:background="#000000">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:text="play"
        android:gravity="center"
        android:textSize="50sp"
        android:id="@+id/one"
        android:layout_alignParentTop="true"
        android:background="@drawable/mode1background"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:textColor="#000000" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:text="play"
        android:gravity="center"
        android:textSize="50sp"
        android:id="@+id/two"
        android:background="@drawable/mode2background"
        android:layout_below="@+id/one"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:textColor="#000000" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:text="play"
        android:gravity="center"
        android:textSize="50sp"
        android:id="@+id/three"
        android:background="@drawable/mode3background"
        android:layout_below="@+id/two"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:textColor="#000000" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:text="play"
        android:gravity="center"
        android:textSize="50sp"
        android:id="@+id/four"
        android:background="@drawable/mode4background"
        android:layout_below="@+id/three"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:textColor="#000000" />

</RelativeLayout>

How can I make each TextView be 1/4 of screen size. 如何使每个TextView成为屏幕大小的1/4。

You could use a vertical LinearLayout with android:layout_weight attributes. 您可以使用具有android:layout_weight属性的垂直LinearLayout

Something like this: 像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#000000"
              android:orientation="vertical">

    <TextView
        android:id="@+id/one"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:background="@drawable/mode1background"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="50sp"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:background="@drawable/mode2background"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="50sp"/>

    <TextView
        android:id="@+id/three"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:background="@drawable/mode3background"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="50sp"/>

    <TextView
        android:id="@+id/four"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:background="@drawable/mode4background"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="50sp"/>

</LinearLayout>

Check out the developer's guide on LinearLayout and Layout Weight: https://developer.android.com/guide/topics/ui/layout/linear.html 查看有关LinearLayout和布局权重的开发人员指南: https : //developer.android.com/guide/topics/ui/layout/linear.html

Change parent layout to LinearLayout with vertical orientation. 将父布局更改为具有垂直方向的LinearLayout Then set each child's height to 0dp and weight to 1 然后将每个孩子的身高设置为0dp并将体重设置为1

Example: 例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="2"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="3"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="4"/>

</LinearLayout>

create the following directories in resourcefolder and place different resolution images of background in respective directories 在resourcefolder中创建以下目录,并将不同分辨率的背景图像放置在各个目录中

drawable-ldpi 可绘制ldpi

drawable-mdpi 绘图分辨率

drawable-hdpi drawable-hdpi

drawable-xhdpi drawable-xhdpi

drawable-xxhdpi drawable-xxhdpi

drawable-xxxhdpi drawable-xxxhdpi

note the name of image in all folders should be same, android will automatically pick the image from the respective folder depending upon the dpi of phone 请注意,所有文件夹中的图片名称应相同,Android会根据手机的dpi自动从相应文件夹中选择图片

As you want to occupy the full height of the device you will need to use linear layout with vertical oreintation and giving attribute 'layout_weight' = 1 for all the TextView. 如果要占据设备的整个高度,则需要使用具有垂直方向的线性布局,并为所有TextView赋予属性'layout_weight'= 1。 Sample code 样例代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical">

    <TextView
        android:id="@+id/one"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:background="@drawable/mode1background"
        android:gravity="center"
        android:text="play"
        android:textColor="#000000"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/two"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/one"
        android:layout_weight="1"
        android:background="@drawable/mode2background"
        android:gravity="center"
        android:text="play"
        android:textColor="#000000"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/three"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/two"
        android:layout_weight="1"
        android:background="@drawable/mode3background"
        android:gravity="center"
        android:text="play"
        android:textColor="#000000"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/four"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/three"
        android:layout_weight="1"
        android:background="@drawable/mode4background"
        android:gravity="center"
        android:text="play"
        android:textColor="#000000"
        android:textSize="50sp" />

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM