gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:34:17 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com