## 視圖view的基本屬性
下面是視圖在代碼中常用的設置方法說明。
* setLayoutParams:設置該視圖的布局參數。參數對象的構造函數可以設置視圖的寬度和高度。參數對象的setMargins方法可以設置該視圖與周圍視圖之間的空白距離。
* setMinimumWidth:設置該視圖的最小寬度。
* setMinimumHeight:設置該視圖的最小高度。
* setBackgroundColor:設置該視圖的背景顏色。
* setBackgroundDrawable:設置該視圖的背景圖片。
* setBackgroundResource:設置該視圖的背景資源id。
* setPadding:設置該視圖邊緣與內部內容之間的空白距離。
* setVisibility:設置該視圖的可視類型。
### margin和padding的區別
margin和padding看字面意思都是空白距離,那么它們到底有什么區別呢?
* (1)margin是指當前視圖與周圍視圖的距離。**外邊距**
* (2)padding是指當前視圖與內部視圖的距離。**內邊距**
示例
~~~
<!-- 最外層的布局背景為藍色 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#00aaff"
android:orientation="vertical"
android:padding="5dp">
<!-- 中間層的布局背景為黃色 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="#ffff99"
android:padding="60dp">
<!-- 最內層的視圖背景為紅色 -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000" />
</LinearLayout>
</LinearLayout>
~~~
如圖所示

### 線性布局
線性布局的常見屬性說明如下:
* orientation:指定線性布局的方向。
horizontal表示水平布局,vertical表示垂直布局。如果不指定該屬性,就默認是horizontal。
* gravity:指定布局內部視圖與本線性布局的對齊方式。
* layout_gravity:指定該視圖與上級視圖的對齊方式,
* layout_weight:指定當前視圖的寬或高占上級線性布局的權重。
示例如下
~~~
<!-- 最外層的布局背景為橙色,它的下級布局在水平方向上依次排列 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#ffff99"
android:orientation="horizontal"
android:padding="5dp">
<!-- 第一個子布局背景為紅色,它與上級布局靠下對齊,它的下級視圖則靠左對齊 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:gravity="left"
android:background="#ff0000"
android:layout_margin="10dp"
android:padding="10dp"
android:orientation="vertical">
<!-- 內層視圖的寬度和高度都是100dp,且背景色為青色 -->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff" />
</LinearLayout>
<!-- 第二個子布局背景為紅色,它與上級布局靠上對齊,它的下級視圖則靠右對齊 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_gravity="top"
android:gravity="right"
android:background="#ff0000"
android:layout_margin="10dp"
android:padding="10dp"
android:orientation="vertical">
<!-- 內層視圖的寬度和高度都是100dp,且背景色為青色 -->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff" />
</LinearLayout>
</LinearLayout>
~~~

### 滾動視圖
手機屏幕的顯示空間有限,常常需要上下滑動或左右滑動才能拉出其余頁面內容,可惜Android的布局節點都不支持自行滾動,這時就要借助ScrollView滾動視圖實現了。
與線性布局類似,滾動視圖也分為垂直方向和水平方向兩類,其中垂直滾動的視圖名是ScrollView,水平滾動的視圖名是HorizontalScrollView。
* (1)垂直方向滾動時,layout_width要設置為match_parent,layout_height要設置為wrap_content。
* (2)水平方向滾動時,layout_width要設置為wrap_content,layout_height要設置為match_parent。
* (3)滾動視圖節點下面必須且只能掛著一個子布局節點,否則會在運行時報錯`Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child`。
示例
~~~
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- HorizontalScrollView是水平方向的滾動視圖,當前高度為200dp -->
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="200dp">
<!-- 水平方向的線性視圖,兩個子視圖的顏色分別為青色和黃色 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="400dp"
android:layout_height="match_parent"
android:background="#aaffff" />
<View
android:layout_width="400dp"
android:layout_height="match_parent"
android:background="#ffff00" />
</LinearLayout>
</HorizontalScrollView>
<!-- ScrollView是垂直方向的滾動視圖,當前高度為自適應 -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 垂直方向的線性視圖,兩個子視圖的顏色分別為綠色和橙色 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#00ff00" />
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#ffffaa" />
</LinearLayout>
</ScrollView>
</LinearLayout>
~~~
>[info]注意:有時ScrollView的實際內容不夠,又想讓它充滿屏幕,怎么辦呢?如果把layout_height屬性賦值為match_parent,那么結果還是不會充滿,正確的做法是再增加一行fillViewport的屬性設置(該屬性為true表示允許填滿視圖窗口),舉例如下:
```
android:layout_height="match_parent"
android:fillViewport="true"
```