繁体   English   中英

如何在android xml中制作响应式布局?

[英]How to make responsive layout in android xml?

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

<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 个设备的结果

在像素 2 api

在此处输入图片说明

这是nexus 4

在此处输入图片说明

图像在 RecyclerView 内。 这是我在我的片段中设置它的方法

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

正如您在像素 2 中看到的那样,下一张图片的几乎一半出现在连接点 4 中。也许这是一个边距。

顺便说一句,我正在使用 android studio 模拟器

我使用 MM 因为这个http://www.mysamplecode.com/2011/06/android-units-of-measurements.html

所以我的问题是,如何使两个设备中的图像大小和边距相同? 我怎样才能实现它?

使用 DP 的更新。

现在,我现在只展示 2 张图片

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

在此处输入图片说明

像素 2

在此处输入图片说明

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

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

第一件事是永远不要使用硬编码来设置任何小部件,使用 match_parent 和 wrap_content 而不是整数。 如果你想在某处使用特定的高度和宽度,那么使用密度像素(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 推荐的简单方法是为不同屏幕尺寸的布局创建一个单独的布局文件。 请检查这个https://developer.android.com/training/multiscreen/screensizes

好的。 感谢所有回答问题的人。 我已经得到了我想要实现的目标(至少它更好)。 这就是我所做的。

首先我改变

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>

最后一个变化是在我的 parentlayout 中。

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

所以这是结果

在此处输入图片说明

左侧是连接点,右侧是像素 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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