简体   繁体   中英

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

I have built a layout in android studio and It appears the same on my AVD but not when i run it in my device. Here's the xml of my layout,

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

Here's my output in android studio, enter image description here

Here's my output when i run it in my Galaxy S9, enter image description here

I have implemented layouts in various densities and sizes(hdpi,mdpi,xhdpi.normal,large,x-large.. etc) but still the result is the same, What am I doing wrong here?

ps-my home screen- enter image description here

You can ConstraintLayout , you could avoid use all those fixed size dimensions on your views and by that make your layout responsive.

Here is an example of using 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>

Notice that I am using this attributes:

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

To decide what size my view should be in percents (and not in fixed-size) according to the screen size. This will make sure that my layout is responsive to all screen size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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