1.UI控件
1.1 TextView控件
常用屬性
屬性 含義 id 給當前控件定義了一個唯 一標識符 layout_width 高度,單位:dp (wrap_content, match_parent) layout_height 寬度,單位:dp (wrap_content, match_parent) background 設置背景圖片 text 指定控件中顯示的文本內容 gravity 來指定文字的對齊方式(top 、bottom 、left 、right 、center) textSize 指定控件中文字的大小 textColor 指定控件中文字的顏色(#000000~#FFFFFF) textStyle 指定控件中文字的樣式
帶陰影的TextView)(Drawable資源文件使用)
屬性 含義 shadowColor 設置陰影顏色,需要與shadowRadius一起使用 shadowRadius 設置陰影的模糊程度,設為0.1就變成字體顏色(建議為3.0) shadowDx 設置陰影在水平方向的偏移,就是水平方向陰影開始的橫坐標位置 shadowDy 設置陰影在水平方向的偏移,就是水平方向陰影開始的橫坐標位置
帶邊框的TextView(Drawable資源文件使用)
通過設置邊框的Drawable文件,然后通過TextView的background調用來實現有邊框的調用
< TextViewandroid: background= " @drawable/txt_rectborder" />
帶圖片的TextView(Drawable資源文件使用)
設置圖片的核心其實就是Drawable
資源文件,它可以設置四個方向的圖片: drawableTop(上)、drawableButtom(下)、drawableLeft(左)、drawableRight(右) 另外,你也可以使用drawablePadding
來設置圖片與文字間的間距!
1.2 EditText控件
常用屬性
屬性 含義 hint 默認提示文本 textColor 設置文本顏色(#000000~#FFFFFF) height 設置控件高度 width 設置控件寬度 textStyle 設置字體樣式(粗體、斜體) selectAllOnFocus 獲取焦點后全選組件內所有文本內容
限制EditText輸入類型
屬性 含義 inputType phone(撥號鍵盤)date(日期鍵盤)time(時間鍵盤)
設置最小行、最多行、單行、多行、自動換行
屬性 含義 singleLine 設置只允許單行輸入,而且不會滾動 minLines 設置最小行數 maxLines 設置最大行數 textScaleX 設置字與字的水平間隔 textScaleY 設置字與字的垂直間隔 capitalize 設置英文字母大寫(sentences【第一個字母大寫】、word【每一個單詞首字母大寫,以空格區分單詞】、characters【每一個英文字母都大寫】)
設置內邊距
屬性 含義 marginTop 上邊距 marginBottom 下邊距 marginRight 右邊距 marginLeft 左邊距
1.3 Button按鈕與ImageView圖像按鈕
常用屬性
屬性 描述 drawable 放一個drawable資源 drawableTop 可拉伸要繪制的文本的上面 drawableBottom 可拉伸要繪制的文本的下面 drawableLeft 可拉伸要繪制的文本的左面 drawableRight 可拉伸要繪制的文本的右面 text 設置顯示文本 textColor 設置顯示文本顏色 textSize 設置顯示文本大小 background 可拉伸使用的背景 onclick 設置點擊事件(不常用)
Button的狀態屬性
屬性 描述 stated_pressed 是否按下,如一個按鈕觸摸或者點擊 state_focused 是否取得焦點,比如用戶選擇了一個文本框 state_hovered 光標是否懸停,通常與focused state相同 state_selected 被選中狀態 state_checkable 組件是否能被check。如:RadioButton是可以被check的 state_checked 被checked了,如:一個RadioButton可以被check了的 state_enabled 能夠接受觸摸或者點擊事件 state_activated 被激活 state_window_focused 應用程序是否在前臺,當有通知欄被拉下來或者一個對話框彈出的時候應用程序就不在前臺了
1.4 ImageView控件的src屬性和background屬性的區別
background
通常指的都是背景 ,而src
指的是內容 !!當使用src
填入圖片時,是按照圖片大小直接填充 ,并不會進行拉伸 ;使用background填入圖片,則是會根據ImageView給的值進行拉伸。
1.5 RadioButton控件(單選按鈕)和CheckBox控件(多選按鈕)
示例說明
< RadioGroupandroid: id= " @+id/radioGroup" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: orientation= " horizontal" > < RadioButtonandroid: id= " @+id/btnMan" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 男" android: checked= " true" /> < RadioButtonandroid: id= " @+id/btnWoman" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 女" />
</ RadioGroup> < CheckBoxandroid: id= " @+id/cb_one" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 香蕉" />
< CheckBoxandroid: id= " @+id/cb_two" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 蘋果" />
< CheckBoxandroid: id= " @+id/cb_three" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 菠蘿" />
1.6 ToggleButton控件(開關按鈕)Switch控件(開關)
ToggleButton常用屬性
屬性 描述 disabledAlpha 設置按鈕在禁用時的透明度 textOff 按鈕沒有被選中時顯示的文字 textOn 按鈕被選中時顯示的文字
Switch常用屬性
屬性 描述 showText 設置on/off的時候是否顯示文字(true / false) splitTrack 是否設置一個間隙,讓滑塊與底部圖片分隔(true / false) switchMinWidth 設置開關的最小寬度 switchPadding 設置滑塊內文字的間隔 switchTextAppearance 設置開關的文字外觀(用得少) textOff 按鈕沒有被選中時顯示的文字 textOn 按鈕被選中時顯示的文字 textStyle 文字風格,粗體,斜體寫劃線那些 track 底部的圖片 thumb 滑塊的圖片 typeface 設置字體(sans, serif, monospace) 自定義字體文件: 1. 將字體文件保存在assets/fonts/目錄下 2. 在Java代碼中設置: Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"); textView.setTypeface(typeFace);
示例說明
< ToggleButtonandroid: id= " @+id/tbtn_open" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: checked= " true" android: textOff= " 關閉聲音" android: textOn= " 打開聲音" /> < Switchandroid: id= " @+id/swh_status" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: textOff= " " android: textOn= " " android: thumb= " @drawable/thumb_selctor" android: track= " @drawable/track_selctor" />
1.7 ProgressBar控件
基本用法
屬性 描述 max 進度條最大值 style 設置進度條的樣式:環形或者水平進度條(Horizontal) progress 進度條已完成進度值 progressDrawable 設置軌道對應的Drawable對象 indeterminate 如果設置成true,則進度條不精確顯示進度 indeterminateDrawable 設置不顯示進度的進度條的Drawable對象 indeterminateDuration 設置不精確顯示進度的持續時間 secondaryProgress 二級進度條,類似于視頻播放的一條是當前播放進度,一條是緩沖進度,前者通過progress屬性進行設置
1.8 SeekBar控件
基本用法
屬性 描述 max 滑動條的最大值 progress 滑動條的當前值 secondaryProgress 二級滑動條的進度 thumb 滑塊的drawable
1.9 RatingBar控件
基本用法
屬性 描述 isIndicator 是否用作指示,用戶無法更改,默認false numStars 顯示多少個星星,必須為整數 rating 默認評分值,必須為浮點數 stepSize 評分每次增加的值,必須為浮點數 style 樣式選擇
2. 布局
Android中有六大布局,分別是:LinearLayout
(線性布局),RelativeLayout
(相對布局),TableLayout
(表格布局) FrameLayout
(幀布局),AbsoluteLayout
(絕對布局),GridLayout
(網格布局)
2.1 線性布局管理(LinearLayout)
屬性 含義 layout_height 高度,單位:dp (wrap_content, match_parent) layout_weight 寬度,單位:dp (wrap_content, match_parent) orietation 方向(vertical,horizontal) gravity 對齊方式(left, right, center, top, bottom…) background 背景(顏色[color]、圖片[drawable]、選擇器[selector]) padding 內邊距 (paddingLeft, paddingRight, paddingTop, paddingBottom) margin 外邊距 (marginLeft, marginRight, marginTop, marginBottom) layout_gravity 本元素相對于父元素的重力方向 orientation 本元素所有子元素的重力方向
權重
分割線
屬性 含義 divider 為LinearLayout設置分割線圖片 showDividers 設置分割線所在位置 dividerPadding 設置分割線的Padding
2.2 相對布局管理(RelativeLayout)
基本屬性
屬性 描述 gravity 設置讓其內組件的對齊方式 ignoreGravity 設置了該屬性為true的屬性的組件,將不受gravity屬性的影響
根據父容器定位
屬性 描述 layout_alignParentLeft 左對齊 layout_alignParentRight 右對齊 layout_alignParentTop 頂部對齊 layout_alignParentBottom 底部對齊 layout_centerHorizontal 水平居中 layout_centerVertical 垂直居中 layout_centerInParent 中間位置
根據兄弟組件定位
屬性 描述 layout_toLeftOf 參考組件的左邊 layout_toRightOf 參考組件的右邊 layout_above 參考組件的上方 layout_below 參考組件的下方 layout_alignTop 對其參考組件的上邊界 layout_alignBottom 對其參考組件的下邊界 layout_alignLeft 對其參考組件的左邊界 layout_alignRight 對其參考組件的右邊界
偏移量(設置組件與父容器的邊距)
屬性 描述 layout_margin 設置組件愛你上下左右的偏移量 layout_marginLeft 設置組件離左邊的偏移量 layout_marginRight 設置組件離右邊的偏移量 layout_marginTop 設置組件離上面的偏移量 layout_marginBottom 設置組件離下面的偏移量
填充(設置組件中內部元素間的邊距)
屬性 描述 padding 往內部元素的上下左右填充一定的邊距 paddingLeft 往內部元素的左邊填充一定的邊距 paddingRight 往內部元素的右邊填充一定的邊距 paddingTop 往內部元素的上面填充一定的邊距 paddingBottom 往內部元素的下面填充一定的邊距
2.3 表格布局管理(TableLayout)
常用屬性
屬性 描述 collapseColumns 設置需要被隱藏的列的序列號 shrinkColumns 設置允許被收縮的列的列序號 stretchColumns 設置運行被拉伸的列的列序號
示例說明
隱藏列
< TableLayout android: id= " @+id/TableLayout2" android: layout_width= " fill_parent" android: layout_height= " wrap_content" android: collapseColumns= " 0,2" > < TableRow> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " one" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " two" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " three" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " four" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " five" /> </ TableRow>
</ TableLayout>
拉伸列
< TableLayout android: id= " @+id/TableLayout2" android: layout_width= " fill_parent" android: layout_height= " wrap_content" android: stretchColumns= " 1" > < TableRow> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " one" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " two" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " three" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " four" /> </ TableRow>
</ TableLayout>
收縮列
< TableLayout android: id= " @+id/TableLayout2" android: layout_width= " fill_parent" android: layout_height= " wrap_content" android: shrinkColumns= " 1" > < TableRow> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " one" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " two" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " three" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " four" /> < Button android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " five" /> < TextView android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 文本XX" /> </ TableRow>
</ TableLayout>
2.4 幀布局管理(FrameLayout)
常用屬性
屬性 描述 foreground 設置改幀布局容器的前景圖像 foregroundGravity 設置前景圖像顯示的位置
// 簡單的幀布局使用
< FrameLayout xmlns: android= " http://schemas.android.com/apk/res/android" xmlns: tools= " http://schemas.android.com/tools" android: id= " @+id/FrameLayout1" android: layout_width= " match_parent" android: layout_height= " match_parent" tools: context= " .MainActivity" android: foreground= " @drawable/logo" android: foregroundGravity= " right|bottom" > < TextView android: layout_width= " 200dp" android: layout_height= " 200dp" android: background= " #FF6143" /> < TextView android: layout_width= " 150dp" android: layout_height= " 150dp" android: background= " #7BFE00" /> < TextView android: layout_width= " 100dp" android: layout_height= " 100dp" android: background= " #FFFF00" /> </ FrameLayout>
2.5 網格布局管理(GridLayout)
設置排列對齊
屬性 描述 orientation 設置排列方式 layout_gravity 設置對其方式
設置幾行幾列
屬性 描述 rowCount 設置行數 columnCount 設置列數
設置組件所在的行列
屬性 描述 layout_row 第幾行,0起 layout_column 第幾列,0起
設置組件橫跨幾行幾列
屬性 描述 rowSpan 縱向跨行數 column 橫向跨行數