Android studio中利用各种页面布局方法完成实验
刚学Android,这是一份实验报告,但同时在实验过程中出现了些问题,希望可以得到帮助
一、实验题目
1.实验目的
(1)掌握界面布局的编写方式;
(2)熟悉常用布局的常用属性设置;
(3)能运用线性布局、相对布局、约束布局实现界面布局。
2.实验要求
(1)分别使用线性布局、相对布局和约束布局实现界面;
(2)界面效果如下:

二、实验步骤(请写出代码,并展示模拟器上输出的界面截图)
1.利用线性布局实现界面。
代码展示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img1"
android:orientation="vertical">
<ImageView
android:layout_width="93dp"
android:layout_height="49dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="15dp"
android:background="@drawable/img2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="立即进入"
android:textColor="@color/colorWhite"
android:layout_marginTop="582dp"/>
</LinearLayout>
模拟器页面效果:

2.利用相对布局实现界面。
代码展示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img1">
<ImageView
android:layout_width="94dp"
android:layout_height="53dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="15dp"
android:background="@drawable/img2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="立即进入"
android:textColor="@color/colorWhite"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
模拟器页面效果:

2.利用相对布局实现界面。
代码展示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
android:background="@drawable/img1">
<ImageView
android:layout_width="90dp"
android:layout_height="43dp"
android:background="@drawable/img2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="立即进入"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
模拟器页面效果:

4.利用帧布局实现界面。
代码展示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img1">
<ImageView
android:layout_width="93dp"
android:layout_height="53dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="15dp"
android:background="@drawable/img2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="646dp"
android:background="@color/colorAccent"
android:text="立即进入"
android:textColor="@color/colorWhite" />
</FrameLayout>
模拟器页面效果:

二、实验总结(实验出现的问题)
对比模拟器界面与预览界面,可以轻易的发现在线性布局以及帧布局两种布局情况下两者下部的button位置并不相同,尝试了许多办法。
例如:
1.创建不同的模拟器尽量调整两者之间的差距
创建了不止两个,这两个是创建的模拟器相比起来是最合适的,其他模拟器比如Nexus 5,最开始甚至会出现上面的图片内容不显示,只有黑色一片区域的情况
2.尝试选择预览界面上方与模拟器相匹配的像素
预览界面像素配置:

模拟器像素配置:

在网上查找了很多,也没有找到解决方案。
因此,想问问看各位技术大佬有没有办法能够帮帮忙,解决一下两者并不匹配的问题,或者有没有出现相同问题的,可以一起来探讨一下!
这是一篇关于Android Studio的实验报告,主要涵盖了线性布局、相对布局和帧布局的实践,以及在不同模拟器中遇到的界面显示不一致问题的总结,特别是线性和帧布局中按钮位置的差异。

3964

被折叠的 条评论
为什么被折叠?



