在写这篇博客之前我要先为我之前的想法道歉,为什么呢,因为我在刚开始使用AS的时候,不管是网上教程还是书上的范例,在写布局文件时,经常使用的是LinearLayout,而AS默认使用的是ConstraintLayout,我每次开始写一个布局文件时,都会特意的去把ConstraintLayout修改成LinearLayout,并且得设置一下布局方向,为此我有时会心理骂这个写ConstraintLayout的人,在此,我觉得我有必要想他或者他们道个歉。
想起看ConstraintLayout的起因
开始我还觉得使用LinearLayout这样写挺好的,省的每次使用ConstraintLayout要为每一个控件设置它所在的方位。直到后来写一些稍微复杂一点的布局,一个LinearLayout里嵌套一个Linearlayout或者RelativeLayout等等,也开始觉得这样写布局文件也好麻烦啊。但是看到前辈们都是用的这些Layout来写,我当时也没感觉有什么,就这样过吧。
前几天开始写第一篇博客,那是从开始建立工程文件到最后build project都是自己一步步的去思考的时候,这时候又想起以前的问题:为什么AS默认的使用的ConstraintLayout布局呢?这才想起去看看,去查一查这些布局的资料。一查发现,有了这个ConstraintLayout,那些LinearLayout、RelativeLayout……都可以“扔了”,实在是太实用了。
不过我还是得说,这个ConstraintLayout对新手可能不是那么友好,或者说对我这种急性子的人不太友好,因为有些时候自己没有写好控件的位置信息,它就会在.xml文件里报错,我这个急性子在刚开始哪还忍得了,直接放弃ConstraintLayout,去使用的Linearlayout,所以我在开始学安卓,以及之后写代码的时候都下意识的把ConstraintLayout当作是一个非常麻烦的东西,也因此没有去深究它为什么是默认的布局,直到这一段时间开始写博客,才开始愿意去花时间去看它的资料。
一个栗子
今天由于时间关系,只是把之前的test工程文件里的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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.a16832.myapplication.MainActivity">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_1"
android:text="button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv_1"/>
<Button
android:id="@+id/btn_2"
android:text="button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/btn_1"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/btn_3"
android:text="button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/tv_1"
app:layout_constraitLeft_toLeftOf="parent"
android:layout_marginBottom="10dp"/>
<Button
android:id="@+id/btn_4"
android:text="button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/btn_3"/>
<Button
android:id="@+id/btn_5"
android:text="button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/btn_3"
app:layout_constraintRight_toLeftOf="@id/btn_4"
app:layout_constraintBottom_toBottomOf="@id/btn_3"/>
<Button
android:id="@+id/btn_6"
android:text="button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv_1"
app:layout_constraintLeft_toRightOf="@id/btn_1"
app:layout_constraintRight_toLeftOf="@id/btn_2"/>
<TextView
android:id="@+id/tv_2"
android:text="thank you all to watch this blog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_2"
android:layout_marginTop="30dp"/>
</>
这里虽然每个控件都需要添加其上下左右的信息,但是它实现的布局却是比较复杂的,理由看右图,里面每个控件之间的纠缠恩怨看起来还是有点麻烦的。(但是如果使用LinearLayout或者RelativeLayout,它可能看起来没这么繁琐,但是写起来我觉得得有一个比较好的大局观或者想象能力,即你的心里得先有这些个控件的具体位置信息,得有一个总览,然后再写会比较方便,但是我们可能会经常遇见一些要求临时加控件的问题,这个就得再考虑这个新控件对整体的影响了,总之感觉略烦)。比如这里要是使用LinearLayout,我是这样想的,可能因为我还是个菜鸟,大家看到别见笑:首先写一个LinearLayout把这8个控件全部包起来,方向设置为垂直,然后再在其中嵌套4个LinearLayout,分别包含着button3、4、5;textview hello world!;button1、2、6;和textview thank you all to watch this blog(应该是thank you all for reading this blog,英语没耍好=·=),然后在这4个LinearLayout里的第一个和第三个给每个button设置权重,让他们均匀分配控件。大概就是这么个意思,感觉说的好模糊。。。。总之挺麻烦的就是了,下面是代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
tools:context=".Main2Activity">
<LinearLayout
android:weightSum="4"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_weight="1"
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:text="btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="btn_2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="btn_3"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="hello world!"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:text="btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:text="BTN_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="btn_6"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_marginTop="20dp"
android:text="thank you all for watching this blog"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
这里看起来它的布局控件之间没有太多恩怨纠缠,看起来挺清爽的,只是我在用过ConstraintLayout后感觉这种就像一本散装的本子,很不牢固,修改某一个控件或者布局的属性可能就会造成很大的影响,而ConstraintLayout就像有胶粘住的本子,这个我现在可能还是因为初学,到底哪个好我不下定论,只等以后的实践再说了。谢谢各位看官读者