Search code examples
layoutaspect-ratio

How to keep the aspect ratio when changing device?


I need to manage my layout with the dp, not with match_parent, or wrap_content.

This is my XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:columnCount="1"
  android:orientation="vertical"
  android:paddingBottom="0dp"
  android:paddingLeft="0dp"
  android:paddingRight="0dp"
  android:paddingTop="0dp"
  android:background="@drawable/backprova"
  android:adjustViewBounds="true"
  tools:context="com.example.settingavatar.MyAvatar" >

   <!-- layer per categoria A -->

  <LinearLayout
    android:id="@+id/llyA"
    android:layout_width="34dp"
    android:layout_height="34dp"

    android:layout_column="0"
    android:layout_marginLeft="110dp"
    android:layout_marginTop="126dp"
    android:layout_gravity="center_horizontal|top"
    android:layout_row="0"

    android:orientation="vertical"
    android:visibility="visible"
    android:background="@drawable/cata1" >
  </LinearLayout>

  <!--anteprima dell'avatar -->

  <ImageView
    android:id="@+id/anteprimaAvatar"
    android:layout_width="145dp"
    android:layout_height="352dp"
    android:layout_weight="1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="43dp"
    android:layout_marginLeft="0dp"
    android:src="@drawable/avatar"
    android:adjustViewBounds="true" />

   <!-- other things -->

</RelativeLayout>

When I change devices, the LinearLayout and ImageView don't keep the proportions with the background image (only the aspect ratio between them).

I think the problems are height and width layout. I have the background image file for every drawable_dpi, but the avatar and the cata1 image only for hdpi, could that be the problem?


Solution

  • In case you want to manage the layout just by using dp, try using an absolute layout, because the basis of using relative layout is to avoid maintaining all the views on dp basis..