Android:XML布局文件詳解


首先在XML布局文件里,會遇到如下一些單位

  px:是屏幕的像素點

  in:英寸

  mm:毫米

  pt:磅,1/72 英寸

  dp:一個基於density的抽象單位,如果一個160dpi的屏幕,1dp=1px

  dip:等同於dp

  sp:同dp相似,但還會根據用戶的字體大小偏好來縮放。

  建議使用sp作為文本的單位,其它用dip

 

Android 界面的基本屬性


布局:
在android 中我們常用的布局方式有這么幾種:
1.LinearLayout ( 線性布局) :(里面只可以有一個控件,並且不能設計這個控件的位置,控
件會放到左上角)線性布局分為水平線性和垂直線性二者的屬性分別為:
android:orientation= "horizontal " android:orientation= "vertical" 。
2.RelativeLayout ( 相對布局) : (里面可以放多個控件,但是一行只能放一個控件)附加幾
類RelativeLayout 的屬性供大家參考:
第一類: 屬性值為true 或false
android:layout_centerHrizontal <!---水平居中--->
android:layout_centerVertical <!---垂直居中--->
android:layout_centerInparent <!---相對於父元素完全居中--->
android:layout_alignParentBottom <!---貼緊父元素的下邊緣--->
android:layout_alignParentLeft <!---貼緊父元素的左邊緣--->
android:layout_alignParentRight <!---貼緊父元素的右邊緣--->
android:layout_alignParentTop <!---貼緊父元素的上邊緣--->
android:layout_alignWithParentIfMissing <!---若找不到兄弟元素以父元素做參照物
--->
第二類:屬性值必須為id 的引用名“ @id/id-name ”
android:layout_below <!---在某元素的下方--->
android:layout_above <!---在某元素的上方--->
android:layout_toLeftOf <!---在某元素的左邊--->
android:layout_toRightOf <!---在某元素的右邊--->
android:layout_alignTop <!---本元素的上邊緣和某元素的的上邊緣
對齊--->
android:layout_alignLeft <!---本元素的左邊緣和某元素的的左邊緣
對齊--->
android:layout_alignBottom <!---本元素的下邊緣和某元素的的下邊緣
對齊--->
android:layout_alignRight <!---本元素的右邊緣和某元素的的右邊緣
對齊--->
第三類:屬性值為具體的像素值,如30dip , 40px
android:layout_marginBottom <!---離某元素底邊緣的距離--->
android:layout_marginLeft <!---離某元素左邊緣的距離--->
android:layout_marginRight <!---離某元素右邊緣的距離--->
android:layout_marginTop <!---離某元素上邊緣的距離--->
3.TableLayout ( 表格布局) : (這個要和TableRow 配合使用,很像html 里面的table)這個
表格布局不像HTML 中的表格那樣靈活,只能通過TableRow 屬性來控制它的行而列的話
里面有幾個控件就是幾列(一般情況)。如:
<TableLayout>
<TableRow>
<EditText></EditText>
<EditText></EditText>
</TableRow>
<TableRow>
<EditText></EditText>
<EditText></EditText>
</TableRow>
</TableLayout>
表示兩行兩列的一個表格。
android:gravity="center" 書面解釋是權重比。其時就是讓它居中顯示。它還可以動態添加里
面的每行每列。如下代碼所示:
/*根據id 查找表格對象*/
TableLayout tableLayout = (TableLayout) findViewById(R.id.table01);
/*創建列對象*/
TableRow tableRow = new TableRow(this);
/*文本框對象*/
TextView temp = new TextView(this);
temp.setText("text 的值");
/*將此文本添加到列中*/
tableRow.addView(temp);
android:stretchColumns="1,2,3,4" 它的意思就是自動拉伸1,2,3,4 列。
4.AbsoluteLayout ( 絕對布局) : (里面可以放多個控件,並且可以自己定義控件的x,y 的
位置)
5.FrameLayout(幀布局):(里面可以放多個控件,不過控件的位置都是相對位置)在它里面的
控件都是按后面的一個控件疊加在前一個控件上來顯示的,所有元素都被放置在最左上角。
如:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:src="@drawable/lotusleaf">
</ImageView>
<ImageView
android:id="@+id/f1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/frog_right"
android:visibility="invisible">
</ImageView>
</FrameLayout>
表示的是id 為f1 的控件疊加在id 為iv1 的控件上面顯示
(LinearLayout 和RelativeLayout 應該又是其中用的較多的兩種。AbsoluteLayout 比較少用,
因為它是按屏幕的絕對位置來布局的如果屏幕大小發生改變的話控件的位置也發生了改變。
這個就相當於HTML 中的絕對布局一樣,一般不推薦使用)
<!--------------------------注意事項:
1 、各布局不要亂用各自的屬性。比如把屬於AbsoluteLayout 布局的android:layout_x 和
android:layout_y 用到LinearLayout 布局或RelativeLayout 布局,或者把RelativeLayout 布
局的below , rightof 等屬性應用到其他布局中。這樣做雖然不會報錯,但這是白浪費感
情的工作,根本達不到我們需要的效果。
2 、關於android:layout_width="fill_parent" 和android:layout_height="wrap_content" ,這是
對每個布局寬和高的設置。wrap_content 可表示隨着其中控件的不同而改變這個布局的寬度
或高度, 類似於自動設置寬和高, fill_parent 使布局填充整個屏幕, 另外還有一種
match_parent ,它本質上和fill_parent 一樣,並從API Level8 開始替代fill_parent 。
--------------------->
TextView 的屬性:
android:autoLink <!---設置是否當文本為URL 鏈接/email/電話號碼/map
時,文本顯示為可點擊的鏈接。可選值(none/web/email/phone/map/all)--->android:autoText
<!---如果設置,將自動執行輸入值的拼寫糾正。此處無效果,在顯示輸入法並輸入的時候起
作用--->
android:bufferType <!---指定getText()方式取得的文本類別。選項editable 類
似於StringBuilder 可追加字符,也就是說getText 后可調用append 方法設置文本內容。
spannable 則可在給定的字符區域使用樣式--->
android:capitalize <!---設置英文字母大寫類型。此處無效果,需要彈出輸入
法才能看得到,參見EditView 此屬性說明--->
android:cursorVisible <!---設定光標為顯示/隱藏,默認顯示--->
android:digits <!---設置允許輸入哪些字符。如“1234567890.+-*/%
()”--->
android:drawableBottom <!---在text 的下方輸出一個drawable,如圖片。如果指定一個顏
色的話會把text 的背景設為該顏色,並且同時和background 使用時覆蓋后者--->
android:drawableLeft <!---在text 的左邊輸出一個drawable,如圖片--->
android:drawablePadding <!---設置text 與drawable(圖片)的間隔,與drawableLeft、
drawableRight、drawableTop、drawableBottom 一起使用,可設置為負數,單獨使用沒有效果
--->
android:drawableRight <!---在text 的右邊輸出一個drawable--->
android:drawableTop <!---在text 的正上方輸出一個drawable--->
android:editable <!---設置是否可編輯--->
android:editorExtras <!---設置文本的額外的輸入數據--->
android:ellipsize <!---設置當文字過長時,該控件該如何顯示。有如下值設
置:”start”— 省略號顯示在開頭;”end” ——省略號顯示在結尾;”middle”—-省略號顯示在中
間;”marquee” ——以跑馬燈的方式顯示(動畫橫向移動)--->
android:freezesText <!---設置保存文本的內容以及光標的位置--->
android:gravity <!---設置文本位置,如設置成“center”,文本將居中顯示
--->
android:hintText <!---為空時顯示的文字提示信息,可通過textColorHint
設置提示信息的顏色。此屬性在EditView 中使用,但是這里也可以用--->
android:imeOptions <!---附加功能,設置右下角IME 動作與編輯框相關的
動作,如actionDone 右下角將顯示一個“完成”,而不設置默認是一個回車符號。這個在
EditView 中再詳細說明,此處無用--->
android:imeActionId <!---設置IME 動作ID--->
android:imeActionLabel <!---設置IME 動作標簽--->
android:includeFontPadding <!---設置文本是否包含頂部和底部額外空白,默認為
true--->
android:inputMethod <!---為文本指定輸入法,需要完全限定名(完整的包
名)。例如:com.google.android.inputmethod.pinyin,但是這里報錯找不到--->
android:inputType <!---設置文本的類型,用於幫助輸入法顯示合適的鍵盤
類型。在EditView 中再詳細說明,這里無效果--->
android:linksClickable <!---設置鏈接是否點擊連接,即使設置了autoLink--->
android:marqueeRepeatLimit <!---在ellipsize 指定marquee 的情況下,設置重復滾動的
次數,當設置為marquee_forever 時表示無限次--->
android:ems <!---設置TextView 的寬度為N 個字符的寬度。這里測
試為一個漢字字符寬度--->
android:maxEms <!---設置TextView 的寬度為最長為N 個字符的寬度。
與ems 同時使用時覆蓋ems 選項--->
android:maxLength <!---限制顯示的文本長度,超出部分不顯示--->
android:lines <!---設置文本的行數,設置兩行就顯示兩行,即使第二
行沒有數據--->
android:maxLines <!---設置文本的最大顯示行數, 與width 或者
layout_width 結合使用,超出部分自動換行,超出行數將不顯示--->
android:minLines <!---設置文本的最小行數,與lines 類似--->
android:lineSpacingExtra <!---設置行間距--->
android:lineSpacingMultiplier <!--設置行間距的倍數。如”$2--->
android:numeric <!---如果被設置,該TextView 有一個數字輸入法。此
處無用,設置后唯一效果是TextView 有點擊效果,此屬性在EdtiView 將詳細說明--->
android:password <!---以小點”.”顯示文本android:phoneNumber 設置為電
話號碼的輸入方式--->
android:privateImeOptions <!---設置輸入法選項,此處無用,在EditText 將進一步討
論--->
android:scrollHorizontally <!---設置文本超出TextView 的寬度的情況下,是否出現橫
拉條--->
android:selectAllOnFocus <!---如果文本是可選擇的,讓他獲取焦點而不是將光標移
動為文本的開始位置或者末尾位置。TextView 中設置后無效果--->
android:shadowColor <!---指定文本陰影的顏色,需要與shadowRadius 一起使
用--->
android:shadowDx <!---設置陰影橫向坐標開始位置--->
android:shadowDy <!---設置陰影縱向坐標開始位置--->
android:shadowRadius <!---設置陰影的半徑。設置為0.1 就變成字體的顏色了,
一般設置為3.0 的效果比較好--->
android:singleLine <!---設置單行顯示。如果和layout_width 一起使用,當文
本不能全部顯示時, 后面用“…” 來表示。如android:text="test_ singleLine "
android:singleLine="true" android:layout_width="20dp"將只顯示“t…”。如果不設置singleLine
或者設置為false,文本將自動換行--->
android:text <!---設置顯示文本.--->
android:textAppearance <!--- 設置文字外觀。如
“ android:attr/textAppearanceLargeInverse”這里引用的是系統自帶的一個外觀, 表示系統是
否有這種外觀, 否則使用默認的外觀。可設置的值如下:
textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/
textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSm
allInverse--->
android:textColor <!---設置文本顏色--->
android:textColorHighlight <!---被選中文字的底色,默認為藍色--->
android:textColorHint <!---設置提示信息文字的顏色,默認為灰色。與hint 一起
使用。--->
android:textColorLink <!---文字鏈接的顏色.--->
android:textScaleX <!---設置文字之間間隔,默認為$2。--->
android:textSize <!---設置文字大小,推薦度量單位”sp”,如”15sp”--->
android:textStyle <!---設置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗
又斜) 2] 可以設置一個或多個,用“|”隔開--->
android:typeface <!---設置文本字體,必須是以下常量值之一: normal 0,
sans 1, serif 2, monospace(等寬字體) 3]--->
android:height <!---設置文本區域的高度,支持度量單位: px(像
素)/dp/sp/in/mm(毫米)--->
android:maxHeight <!---設置文本區域的最大高度--->
android:minHeight <!---設置文本區域的最小高度--->
android:width <!---設置文本區域的寬度,支持度量單位: px(像
素)/dp/sp/in/mm(毫米),與layout_width 的區別看這里--->
android:maxWidth <!---設置文本區域的最大寬度--->
android:minWidth <!---設置文本區域的最小寬度android 布局屬性詳解
RelativeLayout 用到的一些重要的屬性:第一類:屬性值為true 或false--->
android:layout_centerHrizonta <!---水平居中--->
android:layout_centerVertical <!---垂直居中--->
android:layout_centerInparent <!---相對於父元素完全居中--->
android:layout_alignParentBottom <!---貼緊父元素的下邊緣--->
android:layout_alignParentLeft <!---貼緊父元素的左邊緣--->
android:layout_alignParentRight <!---貼緊父元素的右邊緣--->
android:layout_alignParentTop <!---貼緊父元素的上邊緣--->
android:layout_alignWithParentIfMissing <!---如果對應的兄弟元素找不到的話就以父
元素做參照物第二類:屬性值必須為id 的引用名“@id/id-name”--->
android:layout_below <!---在某元素的下方--->
android:layout_above <!---在某元素的的上方--->
android:layout_toLeftOf <!---在某元素的左邊--->
android:layout_toRightOf <!---在某元素的右邊--->
android:layout_alignTop <!---本元素的上邊緣和某元素的的上邊緣對齊--->
android:layout_alignLeft <!---本元素的左邊緣和某元素的的左邊緣對齊--->
android:layout_alignBottom <!---本元素的下邊緣和某元素的的下邊緣對齊--->
android:layout_alignRight <!---本元素的右邊緣和某元素的的右邊緣對齊第三類:屬
性值為具體的像素值,如30dip,40px--->
android:layout_marginBottom <!---離某元素底邊緣的距離--->
android:layout_marginLeft <!---離某元素左邊緣的距離--->
android:layout_marginRight <!---離某元素右邊緣的距離--->
android:layout_marginTop <!---離某元素上邊緣的距離EditText 的android:hint 設
置EditText 為空時輸入框內的提示信息--->
android:gravity <!---屬性是對該view 內容的限定.比如一個button 上
面的text. 你可以設置該text ---> 在view 的靠左, 靠右等位置. 以button 為例,
android:gravity="right"則button 上面的文字靠右android:layout_gravity android:layout_gravity
是用來設置該view 相對與起父view 的位置.比如一個button 在linearlayout 里,你想把該
button 放在靠左、靠右等位置就可以通過該屬性設置. 以button 為例,
android:layout_gravity="right"則button 靠右android:layout_alignParentRight 使當前控件的右
端和父控件的右端對齊。這里屬性值只能為true 或false,默認false。android:scaleType:
android:scaleType 是控制圖片如何resized/moved 來匹對ImageView 的size 。
ImageView.ScaleType / android:scaleType 值的意義區別:CENTER /center 按圖片的原來size
居中顯示,當圖片長/寬超過View 的長/寬,則截取圖片的居中部分顯示CENTER_CROP /
centerCrop 按比例擴大圖片的size 居中顯示,使得圖片長(寬)等於或大於View 的長(寬)
CENTER_INSIDE / centerInside 將圖片的內容完整居中顯示,通過按比例縮小或原來的size
使得圖片長/寬等於或小於View 的長/寬FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到
View 的寬度,居中顯示FIT_END / fitEnd 把圖片按比例擴大/縮小到View 的寬度,顯示在
View 的下部分位置FIT_START / fitStart 把圖片按比例擴大/縮小到View 的寬度,顯示在
View 的上部分位置FIT_XY / fitXY 把圖片 不按比例擴大/縮小到View 的大小顯示
MATRIX / matrix 用矩陣來繪制,動態縮小放大圖片來顯示。** 要注意一點,Drawable 文
件夾里面的圖片命名是不能大寫的
Edittext 的屬性: EditText 繼承關系:View-->TextView-->EditText。EditText 的屬性很
多,這里介紹幾個: android:layout_gravity="center_vertical" <!---設置控
件顯示的位置:默認top,這里居中顯示,還有bottom---> android:hint="請輸入數字!
" <!---設置顯示在空間上的提示信息--->
android:numeric="integer" <!---設置只能輸入整數,如果是小數則是:
decimal---> android:singleLine="true" <!---設置單行輸入,一
旦設置為true , 則文字不會自動換行---> android:password="true"
<!---設置只能輸入密碼--->
android:textColor = "#ff$200" <!---字體顏色--->
android:textStyle="bold" <!---字體,bold, italic, bolditalic--->
android:textSize="20dip" <!---大小--->
android:capitalize = "characters" <!---以大寫字母寫--->
android:textAlign="center" <!---EditText 沒有這個屬性,但TextView 有,
居中--->
android:textColorHighlight="#cccccc" <!---被選中文字的底色,默認為藍色--->
android:textColorHint="#ffff00" <!---設置提示信息文字的顏色,默認為灰色
--->
android:textScaleX="1.5" <!---控制字與字之間的間距--->
android:typeface="monospace" <!--- 字型, normal, sans, serif,
monospace--->
android:background="@null" <!---空間背景,這里沒有,指透明--->
android:layout_weight="1" <!---權重,控制控件之間的地位,在控制控
件顯示的大小時蠻有用的--->
android:textAppearance=" android:attr/textAppearanceLargeInverse"
1.EditText 默認不彈出軟件鍵盤
方法一:
在AndroidMainfest.xml 中選擇哪個activity,設置windowSoftInputMode 屬性為
adjustUnspecified|stateHidden
android:windowSoftInputMode="adjustUnspecified|stateHidden"
方法二:
讓EditText 失去焦點,使用EditText 的clearFocus 方法
edit.clearFocus();
方法三:
強制隱藏Android 輸入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
2.EditText 始終不彈出軟件鍵盤
例:EditText edit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
Button 繼承自VIEW , VIEW 有的屬性它都能用
< xml version="1.0" encoding="utf-8" >
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"> (這里的樣式是當按鈕被按下時的顯示)
<shape>
<gradient
android:startColor="@drawable/gray"
android:endColor="@drawable/white"
android:angle="*"/>
<stroke
android:width="*dp"
android:color="@drawable/teal"/>
<corners
android:radius="*dp"/>
<padding
android:left="**dp"
android:top="*dp"
android:right="**dp"
android:bottom="*dp"/>
</shape>
</item>
<item android:state_focused="true">(這里的樣式是移動到按鈕時的顯示)
<shape>
<gradient
android:startColor="@drawable/silver"
android:endColor="@drawable/springgreen"
android:angle="*"/>
<stroke
android:width="*dp"
android:color="@drawable/teal"/>
<corners
android:radius="*dp"/>
<padding
android:left="**dp"
android:top="*dp"
android:right="**dp"
android:bottom="*dp"/>
</shape>
</item>
<item> (這里的樣式是按鈕正常時的顯示)
<shape>
<gradient
android:startColor="@drawable/silver"
android:endColor="@drawable/snow"
android:angle="*"/>
<stroke
android:width="*dp"
android:color="@drawable/teal"/>
<corners
android:radius="*dp"/>
<padding
android:left="**dp"
android:top="*dp"
android:right="**dp"
android:bottom="*dp"/>
</shape>
</item>
</selector>
<!---注:
<padding
android:left="**dp"
android:top="*dp"
android:right="**dp"
android:bottom="*dp" />
這里left 和right 控制的是Button 上的字體與按鈕的左邊緣和右邊緣的距離,也就是控制
按鈕是長還是短;這里的top 和bottom 控制的是Button 上的字體與按鈕的上邊緣和下邊
緣的距離,也就是控制按鈕時高還是矮。--->
Shape 樣式圓滑效果:
< xml version="1.0" encoding="UTF-8" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color=""/>
<stroke android:width="*dp" android:color=" " />
<padding android:left="*dp" android:top="*dp"
android:right="*dp" android:bottom="*dp"/>
<corners android:radius="*dp"/>
</shape>
CheckBox
RadioGroup
Spinner
TimePicker
ScrollView
ProgressBar
RatingBar
ImageView
ImageButton android:background="#00000000" <!---設置背景圖空白的部分直接透
視背景--->
ImageSwicher&Gallery
GradView
Tab
Menu


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM