【问题标题】:How to make my layout scale on different screen sizes?如何使我的布局在不同的屏幕尺寸上缩放?
【发布时间】:2016-10-24 02:29:42
【问题描述】:

我有简单的布局,您可以玩 4 种不同的关卡/模式。问题是当我在不同的屏幕尺寸上预览布局时,它看起来不一样: Image

这是布局代码:

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

如何使每个TextView 成为屏幕大小的 1/4。

【问题讨论】:

标签: java android android-layout layout textview


【解决方案1】:

您可以使用带有android:layout_weight 属性的垂直LinearLayout

类似这样的:

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

查看LinearLayout 和布局权重的开发者指南: https://developer.android.com/guide/topics/ui/layout/linear.html

【讨论】:

  • 我的 layout_weight 应该像其他答案所建议的那样是 0.25 还是 1?有什么区别,因为它似乎可以与 0.25 和 1 一起使用。
  • 没有关系,只要它们是一样的。我只是更喜欢我的权重总和为 1 :)
  • 和为 4(权重为 1)和 1(权重为 1)和 1(0.25)是不是不一样
  • 不,不是。将其视为“重要性”值。如果权重相等,则Views 将获得相等的空间(它们同样重要)。如果View A 的权重是View B 的权重的两倍,那么View A 将获得两倍与View B. 一样多的空间
  • 有道理 :) 谢谢
【解决方案2】:

将父布局更改为垂直方向的LinearLayout。然后将每个孩子的身高设置为0dp,将体重设置为1

例子:

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

【讨论】:

  • 我的 layout_weight 应该像其他答案所建议的那样是 1 还是 0.25 ?有什么区别,因为它似乎可以与 0.25 和 1 一起使用。
  • 不为父级定义 weightSum 没有区别。
【解决方案3】:

在resourcefolder中创建以下目录,并将不同分辨率的背景图片放在各个目录中

可绘制的 ldpi

可绘制的mdpi

可绘制-hdpi

可绘制-xhdpi

drawable-xxhdpi

drawable-xxxhdpi

注意所有文件夹中的图片名称应该相同,android会根据手机的dpi自动从相应文件夹中选择图片

【讨论】:

    【解决方案4】:

    由于您想占据设备的整个高度,您需要使用具有垂直方向的线性布局,并为所有 TextView 提供属性“layout_weight”=1。 示例代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多