【发布时间】:2019-12-30 14:44:01
【问题描述】:
在 Adobe XD 中制作完所有艺术作品后,我在 Android Studio 中设计了一个布局。主要问题是它在我的 Android Studio 预览菜单中看起来很完美,但是一旦我在智能手机甚至虚拟设备上运行我的项目,原始设计就会变形或无法完全显示在屏幕上。为什么会这样?
我已经按照文档中的说明使用了所有内容。我的意思是,我使用“sp”作为文本。适当的边距和对齐等。
我的 XML 布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:background="@android:color/white">
<ImageView
android:id="@+id/imageView"
android:layout_width="365dp"
android:layout_height="225dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:src="@drawable/pics"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="325dp"
android:layout_height="152dp"
android:layout_marginStart="33dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="34dp"
android:fontFamily="@font/ebrima"
android:text="Investigate comedy scenes for clues,
bring the suspects in for questioning
and analyze evidence to catch the
killers. Are you ready to prove your
detective skills?"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="162dp"
android:fontFamily="@font/stencil"
android:text="CARTOON CRATE"
android:textColor="#000000"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<Button
android:id="@+id/button"
android:layout_width="108dp"
android:layout_height="43dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/btnshape"
android:text="Let's Go!"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.878"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.153" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="@font/typo_round"
android:text="Terms and Conditions"
android:textColor="#EBC500"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.106" /></android.support.constraint.ConstraintLayout>
你可以看到
Android Studio Preview 中的实际设计呈现什么
VS
布局设计在我的智能手机设备上的显示效果。
【问题讨论】:
-
您好 Haseeb,欢迎来到 SO。关于您的布局的一些信息;您正在硬编码大量数字并将所有这些边距和大小添加到小部件;乍一看,这并不总是一个坏主意,但在这种情况下,基于非常少的可见信息,您真正的智能手机的屏幕和密度与模拟器/预览不同。这意味着布局很可能不适合您要求的空间。是的,您使用的是 DP 而不是 PX(很好!),但这并不意味着在 不同 密度下,可用空间将是相同的(纵横比也不相同)。
标签: android-studio android-layout user-interface