更换设备时如何保持宽高比?

时间:2014-10-16 08:48:09

标签: layout aspect-ratio

我需要使用dp管理布局,而不是match_parentwrap_content

这是我的XML代码:

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

当我更改设备时,LinearLayoutImageView不会保留背景图像的比例(仅限于它们之间的宽高比)。

我认为问题是高度和宽度布局。 我有每个drawable_dpi的背景图像文件,但只有hdpi的头像和cata1图像,这可能是问题吗?

1 个答案:

答案 0 :(得分:1)

如果您只想使用dp管理布局,请尝试使用绝对布局,因为使用相对布局的基础是避免在dp基础上维护所有视图。