繁体   English   中英

通过设备运行USB调试时,UI会失真,但在AVD上可以正常工作

[英]My UI gets distorted when I run it through my device(USB debugging) but works fine on my AVD

我在android studio中建立了一个布局,它在我的AVD上看起来一样,但是当我在设备上运行它时却不一样。 这是我的布局的xml,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mLayout"
android:layout_width="match_parent"
tools:context=".toggled_meditate"
android:layout_height="match_parent"
>

<Button
    android:id="@+id/togglemeditate"
    android:layout_width="197dp"
    android:layout_height="244dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="197dp"
    android:layout_marginLeft="197dp"
    android:layout_marginTop="134dp"
    android:layout_marginEnd="17dp"
    android:layout_marginRight="17dp"
    android:layout_marginBottom="281dp"
    android:background="@drawable/togglebutton"
    android:transitionName="toggle" />

<ImageView
    android:id="@+id/togglehome"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="0dp"
    android:layout_marginBottom="0dp"
    android:background="@drawable/actionscreen"
    android:transitionName="imageTransition" />
</RelativeLayout>

这是我在android studio中的输出,请在此处输入图片描述

这是我在Galaxy S9中运行它时的输出,请在此处输入图像描述

我已经实现了各种密度和尺寸(hdpi,mdpi,xhdpi.normal,大,x-large等的布局),但结果仍然相同,这是我做错了什么?

ps-我的主屏幕- 在此处输入图片描述

您可以使用ConstraintLayout ,可以避免在视图上使用所有这些固定大小的尺寸,从而使布局具有响应性。

这是使用ConstraintLayout的示例:

<?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">


<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="Button"
    app:layout_constraintWidth_percent=".5"
    app:layout_constraintHeight_percent=".5"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintEnd_toStartOf="parent"
    app:layout_constraintStart_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="parent" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    android:scaleType="fitXY"
    tools:srcCompat="@tools:sample/avatars[12]" />

</android.support.constraint.ConstraintLayout>

请注意,我正在使用以下属性:

 app:layout_constraintWidth_percent=".5"
 app:layout_constraintHeight_percent=".5"

要确定我的视图应根据屏幕大小以百分比(而不是固定大小)为单位。 这将确保我的布局能够响应所有屏幕尺寸。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM