gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:49:40 24 4
gpt4 key购买 nike

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

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

enter image description here

这是nexus 4

enter image description here

图像在 RecyclerView 中。这是我在 fragment 中设置它的方式

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

正如您在像素 2 中看到的那样,下一张图片几乎有一半显示在 nexus 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

enter image description here

像素 2

enter image description here

最佳答案

I have Used the constraint layout to Implement your requirement.

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

关于android - 如何在android xml中制作响应式布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51034127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com