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

标签 java android android-layout layout textview

我的布局很简单,有 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。

最佳答案

您可以使用具有 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

关于java - 如何使我的布局在不同的屏幕尺寸上缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963093/

相关文章:

java - Prefuse图表手动设置力参数

java - 如何停止在每次 Activity 调用时重新创建选项卡

java - 在 ACTION_MOVE 上切换坐标

android - 使 surfaceView 进入全屏

android - 我如何调整空间和文件的整体外观,使其看起来更像图片?

java - 复制构造函数和继承

java - HBase:MiniDFSCluster.java 在某些环境中失败

java - Bootstrap Classloader加载的类

android - 菜单键在 AppCompat 中不起作用

android - 在客户端和服务器之间同步数据