当前位置: 首页 > 知识库问答 >
问题:

Android Studio Constraint布局预览与手机屏幕不同

鞠建安
2023-03-14

我正在尝试在android studio中编写一个活动,至少在任何设备中都是类似的。为了实现这一点,我已经在使用:

  • 约束布局
  • 文本大小为sp,其他大小为dp
  • 在我的android预览版和我的物理设备中使用相同的分辨率(1920x1080)

但是,我在每个屏幕上都有不同的屏幕行为,正如你可以从图片中看到的。

主要区别在于:

  • 文本大小似乎不同,即使使用sp单位来定义它;
  • 手机有换行符,但没有预览;
  • 与手机相比,预览有更大的空白空间。

发生了什么事?如何防止这种行为,并为不同的手机屏幕设计类似的布局?

xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp" >

        <LinearLayout
            android:background="@color/colorWhite"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="45dp"
            android:layout_marginBottom="45dp">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingLeft="30dp"
                    android:paddingRight="30dp">

                    <TextView
                        android:id="@+id/templatetv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView1"
                        android:textColor="@color/colorPrimary"
                        android:textStyle="bold"
                        android:textSize="25sp"
                        android:layout_marginTop="30dp" />

                    <TextView
                        android:id="@+id/textview1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Name: John Sample"
                        android:textSize="18sp"
                        android:layout_marginTop="10dp" />

                    <TextView
                        android:id="@+id/textview2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Document number: XXX.XXX.XXX-XX"
                        android:textSize="18sp"
                        android:layout_marginTop="10dp" />

                    <TextView
                        android:id="@+id/textview3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Father name: John Sample"
                        android:textSize="18sp"
                        android:layout_marginTop="10dp" />

                    <TextView
                        android:id="@+id/textview4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Mother name: Eva Sample"
                        android:textSize="18sp"
                        android:layout_marginTop="10dp" />

                    <TextView
                        android:id="@+id/textview5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Birthdate: 01/01/1901"
                        android:textSize="18sp"
                        android:layout_marginTop="10dp" />

                </LinearLayout>
            </ScrollView>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

Android Studio屏幕预览手机屏幕

共有2个答案

董弘新
2023-03-14

您需要为不同的屏幕定义不同的布局。此外,如果你遵循材料设计指南,大多数问题都不会出现。

欧阳学真
2023-03-14

试着去掉这部分,我想可以是边距:

    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp" >
 类似资料:
  • 我试图用swXXXdp布局文件为不同的屏幕大小创建不同的布局。不幸的是,我仍然不能分开5,0“和6,3”作为可见的附上的图像。两种大小仍然使用相同的文件:layout-sw412dp。对此有什么想法吗?

  • 我一直在我的5.2英寸设备(LG G2)上开发和测试我的应用程序。 我刚刚在一个更大的5.5英寸设备(OnePlus 3T)上启动了该应用程序,它看起来不太好,如下所示: 这是主要活动: 这是子项的布局xml: 我在这里做错了什么? 编辑: 我搜索了一点,找到了一个解决方案,使用以下代码动态设置GridView的最小高度: 此外,我在适配器中添加了以下代码行: 在我的例子中,我必须将屏幕高度除以2

  • 手机预览 功能介绍:重新设置店铺名称以及查看手机预览图。 步骤 从【店铺设置】→【基础设置】→【手机预览】。

  • 我创建了一个相机应用程序,我希望我的应用程序在所有4种可能的方向上打开,并相应地更新相机预览。为此,我使用了以下我复制的方法:Android-相机预览是横向的 我已经测试了这些值,并在三星galaxy tab2上进行了测试,最终获得了正确的方向,一切正常。当我在htc one的手机上试用时,它根本不起作用!!!!!所有的e方向都是完全错误的!所以我得出结论,thre必须是2种类型的设备(或更多…请

  • 我有这个代码: 它给了我这个: 它应该是一个8x8的网格,但它已经溢出了。如何以非编程方式(通过xml)拟合64个方块?

  • 所以我是一个计算机科学的学生,我已经完成了我的第一年。我想创建一个简单的程序,我意识到我已经厌倦了使用无布局; 给每一个单独的组件添加界限是如此的烦人。嗯,我一直在大量使用组件和,这使我的工作变得更容易了。但我已经厌倦了。 我非常关心我制作的GUI的外观,在开始添加代码的功能之前,我几乎用了一半的时间来编程使GUI看起来很好。通过不使用布局并添加边界,我被迫使用,因为如果更改JFrame的大小,这