这篇文章主要是介绍线性布局和约束布局的一些常用属性,以及textview的一些常用的用法。主要还是得多实践。
(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:orientation="vertical"//设置布局是垂直布局还是横向布局
android:paddingTop="20dp"//设置布局管理器内组件距离父物体顶部的距离>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world "
></TextView>
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:padding="20dp"
android:orientation="vertical"
android:gravity="center"//设置布局管理器内的组件的对齐方向
>
<View
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_weight="1"
android:background="@color/black"
></View>
<View
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_weight="1"
android:background="@color/black"
android:layout_marginTop="30dp"//设置距离上个组件top端的位置
></View>
</LinearLayout>
</LinearLayout>
我们通过这段代码中不难发现,其实margin和padding属性是有些类似的,并且都可以设置上下左右的距离,我们可以记住大概的意思就可以了,主要还是得多实践,多实践,多实践,重要的事说三遍!!!
(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:gravity="center"
android:paddingTop="20dp">
<Button
android:id="@+id/btm"//每个组件都设置一个属于自己的id,然后布局的话我们可以减少嵌套布局,可以根据周围的组件来进行布局。
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:text="Button"></Button>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_below="@id/btm"
android:layout_marginTop="10dp"
android:background="#ffff00"
android:gravity="center">
<TextView
android:id="@+id/t1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="textview1"
android:textSize="20dp"></TextView>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/t1"
android:layout_marginTop="10dp"
android:text="textview1"></TextView>
<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/t2"
android:layout_marginTop="10dp"
android:text="textview1"></TextView>
</RelativeLayout>
</RelativeLayout>
对相对布局的看法:最大的区别就是可以给每个组件一个id,可以根据周围的控件来进行布局,当然,线性布局和相对布局之间是可以相互嵌套的,可以更灵活吧,这里可能对相对布局介绍的简单了点,我们后续会用到什么,再补充进来。
(3)小结
这两种布局可能没有约束布局好用,个人认为,但是网上的老师,和学校的老师都说,这两种布局是现在常用的两种,我们就先学好这两种布局,用好这两种布局,到时候上手约束布局就会很容易了(约束布局yyds),我在下个礼拜准备用这两个布局写一个界面,可能会用到更多的东西,尽请期待!