如何在androidxml中做出响应性布局?

如何在androidxml中做出响应性布局?,android,android-layout,Android,Android Layout,我正在尝试创建一个包含图像的简单布局。这就是我现在要做的 <RelativeLayout android:layout_marginStart="2mm" android:layout_marginTop="20sp" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height=

我正在尝试创建一个包含图像的简单布局。这就是我现在要做的

<RelativeLayout
    android:layout_marginStart="2mm"
    android:layout_marginTop="20sp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="27mm"
        android:layout_height="30mm"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
         />

</RelativeLayout>
正如你在像素2中看到的,Nexus4中显示的是下一张图片的几乎一半也许这是一个差额

顺便说一句,我正在使用android studio emulator

我用MM是因为这个

所以我的问题是,如何使两台设备的图像大小和边距相同?我怎样才能做到呢

使用DP的更新

现在,我只展示了两张图片

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

    android:layout_marginTop="20sp"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="170dp"
        android:layout_height="170dp"

         />

</RelativeLayout>

结果

Nexus 4

像素2


第一件事是永远不要使用硬编码来设置任何小部件使用匹配父项和换行内容而不是整数。
如果您想在某处使用特定的高度和宽度,请使用密度像素(dp)而不是毫米和厘米,并使用(sp)作为文本。

静态高度宽度在所有设备中都不支持

将imageview宽度和高度更改为“match_parent”,并尝试scaleType以适应屏幕

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/icon" />

我已经使用约束布局来实现您的需求


android推荐的简单方法是为不同屏幕大小的布局创建一个单独的布局文件。 请看这个
好的。感谢所有回答这个问题的人。我已经得到了我想要的(至少更好)。这就是我要做的

首先我要换衣服

layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
myListArrival.setLayoutManager(layoutManager);

第二个变化是在我的XML中。这是变化

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

android:layout_marginTop="20sp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/myImage"
    android:layout_width="170dp"
    android:layout_height="170dp"
    />

</RelativeLayout>

最后一个变化是在我的父布局中

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/linearHot"
        android:layout_marginBottom="3dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="3dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10sp"
            android:text="Our Products"
            android:textColor="@color/colorAccent"
            android:textSize="15sp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/product_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" />

    </LinearLayout>

左侧为nexus,右侧为像素2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#000"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_marginTop="4dp"
            android:background="#fff"
            android:layout_height="1dp" />
    <ImageView
        android:layout_width="match_parent"
        android:background="@drawable/ww1"
        android:scaleType="center"
        android:id="@+id/walpaper1"
        android:layout_gravity="center"
        android:layout_height="200dp" />
</LinearLayout>

</RelativeLayout>

尝试使用dp而不是mm@ישואובאותך我正在尝试使用dp请检查我的更新问题
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

android:layout_marginTop="20sp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/myImage"
    android:layout_width="170dp"
    android:layout_height="170dp"
    />

</RelativeLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/linearHot"
        android:layout_marginBottom="3dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="3dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10sp"
            android:text="Our Products"
            android:textColor="@color/colorAccent"
            android:textSize="15sp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/product_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" />

    </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#000"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_marginTop="4dp"
            android:background="#fff"
            android:layout_height="1dp" />
    <ImageView
        android:layout_width="match_parent"
        android:background="@drawable/ww1"
        android:scaleType="center"
        android:id="@+id/walpaper1"
        android:layout_gravity="center"
        android:layout_height="200dp" />
</LinearLayout>

</RelativeLayout>