控件与布局的关系
一个丰富的界面是由很多个控件组成的,那么我们如何才能让各个控件都有条不紊地摆放在界面上,而不是乱糟糟的呢?
这就需要借助布局来实现了。布局是一种可用于放置很多控件的容器,它可以按照一定的规律调整内部控件的位置,从而编写出精美的界面。
当然,布局的内部除了放置控件外,也可以放置布局,通过多层布局的嵌套,我们就能够完成一些比较复杂的界面实现。
LinearLayout
LinearLayout又称作线性布局,这个布局会将它所包含的控件在线性方向上依次排列。
<?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=".MainActivity">
<Button
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:id="@+id/button1"
android:text="Relative Demo"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:id="@+id/button2"
android:text="Frame Demo"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:id="@+id/button3"
android:text="Button 3"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
- LinearLayout垂直排列的效果。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android&#