我正在开发一个新的应用程序,预览显示Android Studio中的不同UI,而实际设备显示不同的UI是不同的,如何在真实设备中显示屏幕与预览中的相同。 正如您在实际设备上看到的那样,缺少跳过文本
在我的XML文件下面。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:attrs="http://schemas.android.com/apk/res-auto"
>
<ImageView
android:id="@+id/vector_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="@drawable/vectors_1"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
<ImageView
android:id="@+id/some_id"
android:layout_width="221dp"
android:layout_height="158dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/doctor_appointment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.442"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.233" />
<TextView
android:id="@+id/search_doct"
android:layout_width="272dp"
android:layout_height="82dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="54dp"
android:layout_alignParentTop="true"
android:layout_marginTop="292dp"
android:gravity="top"
android:text="@string/search_doct"
android:textAppearance="@style/search_doct"
app:layout_constraintBottom_toBottomOf="@id/some_id"
app:layout_constraintHorizontal_bias="0.697"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/vector_1"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/get_list_of"
android:layout_width="217dp"
android:layout_height="58dp"
app:layout_constraintTop_toBottomOf="@id/search_doct"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal|top"
android:text="@string/get_list_of"
android:textAppearance="@style/get_list_of"
/>
<com.rd.PageIndicatorView
android:id="@+id/pageIndicatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.477"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.801"
app:piv_animationType="scale"
app:piv_dynamicCount="true"
app:piv_interactiveAnimation="true"
app:piv_selectedColor="@color/gray_50"
app:piv_unselectedColor="@color/gray_300"
app:piv_viewPager="@id/viewPager"
attrs:piv_padding="12dp"
attrs:piv_radius="8dp" />
<TextView
android:id="@+id/skip"
android:layout_width="87dp"
android:layout_height="43dp"
android:layout_marginTop="636dp"
android:gravity="center_horizontal|top"
android:text="Skip"
android:textAppearance="@style/skip"
app:layout_constraintHorizontal_bias="0.537"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在Android Studio中的预览窗口下方
我想知道我在哪里犯错,任何帮助将不胜感激。
答案 0 :(得分:0)
您的错误是在视图和边距上使用固定的大小,因为不同的手机具有不同的屏幕大小,您可能会发现预览版面看起来很糟糕,而在真实设备上它将看起来完全不同。
您可以像这样使用约束布局percentage来解决此问题:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ImageView
android:id="@+id/vector_1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="@drawable/profile_icon"
app:layout_constraintBottom_toTopOf="@+id/some_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/some_id"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/tasks_icon"
app:layout_constraintBottom_toTopOf="@+id/search_doct"
app:layout_constraintEnd_toEndOf="@+id/vector_1"
app:layout_constraintStart_toStartOf="@+id/vector_1"
app:layout_constraintTop_toBottomOf="@+id/vector_1" />
<TextView
android:id="@+id/search_doct"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="top"
android:text="search_doct"
app:layout_constraintBottom_toTopOf="@+id/get_list_of"
app:layout_constraintEnd_toEndOf="@+id/vector_1"
app:layout_constraintStart_toStartOf="@+id/vector_1"
app:layout_constraintTop_toBottomOf="@+id/some_id" />
<TextView
android:id="@+id/get_list_of"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintWidth_percent="0.5"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal|top"
android:text="get_list_of"
app:layout_constraintBottom_toTopOf="@+id/pageIndicatorView"
app:layout_constraintEnd_toEndOf="@+id/vector_1"
app:layout_constraintStart_toStartOf="@+id/vector_1"
app:layout_constraintTop_toBottomOf="@+id/search_doct" />
<Button
android:id="@+id/pageIndicatorView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintDimensionRatio="1:1"
android:layout_centerInParent="true"
app:layout_constraintBottom_toTopOf="@+id/skip"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/get_list_of" />
<TextView
android:id="@+id/skip"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintWidth_percent="0.5"
android:gravity="center_horizontal|top"
android:text="Skip"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/vector_1"
app:layout_constraintStart_toStartOf="@+id/vector_1"
app:layout_constraintTop_toBottomOf="@+id/pageIndicatorView" />
</androidx.constraintlayout.widget.ConstraintLayout>
它看起来像这样: