【发布时间】:2020-10-26 17:54:40
【问题描述】:
我正在尝试在 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>
【问题讨论】:
-
您的预览和物理设备的屏幕可能具有不同的像素密度。也就是说,即使它们都是 1920x1080
px,它们在dp方面的分辨率也可能非常不同。
标签: android xml android-studio android-layout android-constraintlayout