代码之家  ›  专栏  ›  技术社区  ›  YVS1102

如何在android xml中进行响应式布局?

  •  0
  • YVS1102  · 技术社区  · 8 年前

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

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

    enter image description here

    这是Nexus4

    enter image description here

    图像在RecyclerView中。以下是我如何在片段中设置的

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

    正如您在像素2中看到的,下一张图片中几乎有一半出现在nexus 4中。 也许吧 这是保证金。

    顺便说一下,我正在使用android工作室模拟器

    我用m m是因为这个 http://www.mysamplecode.com/2011/06/android-units-of-measurements.html 是的。

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

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

    结果

    关系4

    enter image description here

    像素2

    enter image description here

    5 回复  |  直到 8 年前
        1
  •  1
  •   Jaspreet Kaur    8 年前

    我使用了约束布局来实现您的需求。

    <?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"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/imageView2"
                android:src="@mipmap/ic_launcher"/>
    
            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/imageView1"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:src="@mipmap/ic_launcher"/>
        </android.support.constraint.ConstraintLayout>
    
        2
  •  0
  •   Sharad Pareek    8 年前

    第一件事是永远不要使用硬编码来设置任何小部件使用match_parent和wrap_content而不是整数。 如果要在某处使用特定的高度和宽度,请使用密度像素(dp)而不是mm和cm,并对文本使用(sp)。

        3
  •  0
  •   Android Thanh Cao    8 年前

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

    将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" />
    
        4
  •  0
  •   Sháilèndra Wregmi    8 年前

    android推荐的简单方法是为不同屏幕大小的布局创建一个单独的布局文件。 请看看这个 https://developer.android.com/training/multiscreen/screensizes

        5
  •  0
  •   YVS1102    8 年前

    好啊。感谢所有回答问题的人。我已经得到了我想要的(至少更好)。这是我的工作。

    我先换衣服

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

    mGridLayoutManager = new GridLayoutManager(getActivity(), 2);
    myListArrival.setLayoutManager(mGridLayoutManager);
    

    第二个变化是在我的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>
    

    结果是

    enter image description here

    左侧为nexus,右侧为像素2