第二章五大布局_第1頁
第二章五大布局_第2頁
第二章五大布局_第3頁
第二章五大布局_第4頁
第二章五大布局_第5頁
已閱讀5頁,還剩23頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第二章五大布局線性布局(LinearLayout)1.LinearLayout屬性:android:gravity=“center”控件中內(nèi)容相對控件的對齊方式的設(shè)置android:layout_gravity=“center”控件相對于父view對齊方式的設(shè)置marginLeft左邊間隔距離marginRight右邊間隔距離marginBottom下邊間隔距離marginTop上邊間隔距離background背景顏色或圖片layout_weight權(quán)重比率layout_width控件的寬度fill_layoutmatch_layoutwrap_layoutlayout_height控件的高度fill_layoutmatch_layoutwrap_layout(1)fill_parent-1Theviewshouldbeasbigasitsparent(minuspadding).ThisconstantisdeprecatedstartingfromAPILevel8andisreplacedbymatch_parent.

(2)match_parent-1Theviewshouldbeasbigasitsparent(minuspadding).IntroducedinAPILevel8.

(3)wrap_content-2Theviewshouldbeonlybigenoughtoencloseitscontent(pluspadding).線性布局(LinearLayout相對布局(RelativeLayout)相對布局(RelativeLayout)第一組layout相對布局屬性

android:layout_below

android:layout_toRightOf移動到指定控件的右邊Androidlayout_toLeftOf="@id/btn1"移動到指定控件的左邊android:layout_below="@id/btn1"移動到指定控件的下邊android:layout_above="@id/btn1"移動到指定控件的上邊注:此屬性時寫在控件標(biāo)簽內(nèi)部,用于控制控件的浮動,類似于CSS中的float屬性.相對布局(RelativeLayout)第二組Layout相對布局屬性

android:layout_alignLeft

android:layout_alignRight

android:layout_alignTop

android:layout_alignBottom注:此組屬性時控制控件的對齊方式,分別對應(yīng)為左對齊右對齊上對齊和下對齊相對布局(RelativeLayout)第三組layout相對布局屬性

android:layout_alignBaseLine注:此屬性是讓控件中的text(僅限于拉丁語系字母)以基準線為標(biāo)準對齊.相對布局(RelativeLayout)第四組layout相對布局屬性:android:layout_centerInParent="true"移動到父容器的中間layout_alignParentTop="true"移動到父容器的上面android:layout_alignParentBottom=""移動到父容器的下面android:layout_alignParentLeft=""移動到父容器的左android:layout_alignParentRight=""移動到父容器的右練習(xí)表格布局(TableLayout)按行和列來進行排列<TableRow>每一行幀布局(FrameLayout)一層一層可以疊加的排列方式絕對布局(AbsoluteLayout)根據(jù)X軸和Y軸來進行排列

Shape使用

在Android程序開發(fā)中,我們經(jīng)常會去用到Shape這個東西去定義各種各樣的形狀

corners:圓角android:radius為角的弧度,值越大角越圓。

我們還可以把四個角設(shè)定成不同的角度,同時設(shè)置五個屬性,則Radius屬性無效android:Radius="20dp"

設(shè)置四個角的半徑android:topLeftRadius="20dp"

設(shè)置左上角的半徑

android:topRightRadius="20dp"

設(shè)置右上角的半徑

android:bottomLeftRadius="20dp"

設(shè)置右下角的半徑

android:bottomRightRadius="20dp"

設(shè)置左下角的半徑

padding:間隔可以設(shè)置上下左右四個方向的間隔<padding

android:bottom="10dp"

android:left="10dp"

android:right="10dp"

android:top="10dp"

/>

<!--

設(shè)置各個方向的間隔

-->

stroke:描邊android:width="2dp"描邊的寬度,android:color描邊的顏色。

我們還可以把描邊弄成虛線的形式,設(shè)置方式為:

android:dashWidth="5dp"

android:dashGap="3dp"

其中android:dashWidth表示'-'這樣一個橫線的寬度,android:dashGap表示之間隔開的距離

gradient:漸變android:startColor和android:endColor分別為起始和結(jié)束顏色,android:angle是漸變角度,必須為45的整數(shù)倍。

另外漸變默認的模式為android:type="linear",即線性漸變,可以指定漸變?yōu)閺较驖u變,android:type="radial",徑向漸變需要指定半徑android:gradientRadius="50"。

angle值對應(yīng)的位置如圖:solid:填充android:color指定填充的顏色

案例在drawable文件夾下面先定義兩個xml文件:button_bg.xml的內(nèi)容如下:<?xml

version="1.0"

encoding="utf-8"?>

<shape

xmlns:android="/apk/res/android"

>

<!--

填充

-->

<solid

android:color="#ff9d77"

/>

<!--

定義填充的顏色值

-->

<!--

描邊

-->

<stroke

android:width="2dp"

android:color="#fad3cf"

/>

<!--

定義描邊的寬度和描邊的顏色值

-->

<!--

圓角

-->

<corners

android:bottomLeftRadius="5dp"

android:bottomRightRadius="5dp"

android:topLeftRadius="5dp"

android:topRightRadius="5dp"

/>

<!--

設(shè)置四個角的半徑

-->

<!--

間隔

-->

<padding

android:bottom="10dp"

android:left="10dp"

android:right="10dp"

android:top="10dp"

/>

<!--

設(shè)置各個方向的間隔

-->

</shape>

button_pressed_bg.xml的內(nèi)容如下:<?xml

version="1.0"

encoding="utf-8"?>

<shape

xmlns:android="/apk/res/android"

>

<!--

漸變

-->

<gradient

android:endColor="#FFFFFF"

android:gradientRadius="50"

android:startColor="#ff8c00"

android:type="radial"

/>

<!--

描邊

-->

<stroke

android:dashGap="3dp"

android:dashWidth="5dp"

這里說明一點,在描邊里面設(shè)置了dash參數(shù),使得圖形的邊變成了虛線

android:width="2dp"

android:color="#dcdcdc"

/>

<!--

圓角

-->

<corners

android:radius="5dp"

/>

<!--

間隔

-->

<padding

android:bottom="10dp"

android:left="10dp"

android:right="10dp"

android:top="10dp"

/>

</shape>

在drawable文件夾下添加一個button.xml文件,內(nèi)容如下:<?xml

version="1.0"

encoding="utf-8"?>

<selector

xmlns:android="/apk/res/android">

<item

android:drawable="@drawable/button_pressed_bg"

android:state_pressed="true"></item>

<item

android:drawable="@drawable/button_bg"></item>

</selector>

這個文件的意思以前講過,normal(正常)情況下就顯示button_bg,被press(按下)情況下就顯示button_pressed_bg。

layout目錄下的activity_main.xml的內(nèi)容:<RelativeLayout

xmlns:android="/apk/res/android"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/button"

android:text="TestShapeButton"

/>

</RelativeLayout>

直接將background指定為drawable文件夾下的button.xml。

使用步驟第一步:首先在res目錄下新建drawable文件夾

第二步:在drawable文件夾中創(chuàng)建xml文件android背景選擇器selector用法android背景選擇器selector用法selector可以設(shè)置的屬性有:

android:state_pressed

如果是true,當(dāng)被點擊時顯示該圖片,如果是false沒被按下時顯示默認。

android:state_focused

如果是true,獲得焦點時顯示;如果是false沒獲得焦點顯示默認。

android:state_selected

如果是true,當(dāng)被選擇時顯示該圖片;是false未被選擇時顯示該圖片。

android:state_checkable

如果值為true,當(dāng)CheckBox能使用時顯示該圖片;false,當(dāng)CheckBox不能使用時顯示該圖片。

android:state_checked

如果值為true,當(dāng)CheckBox選中時顯示該圖片;false,當(dāng)CheckBox為選中時顯示該圖片。

android:state_enabled

如果值為true,當(dāng)該組件能使用時顯示該圖片;false,當(dāng)該組件不能使用時顯示該圖片。

android:state_window_focused

如果值為true,當(dāng)此activity獲得焦點在最前面時顯示該圖片;false,當(dāng)沒在最前面時顯示該圖片。

案例一個Button控件的背景設(shè)置為:

<?xmlversion="1.0"encoding="utf-8"?>

<selectorxmlns:android="/apk/res/android">

<itemandroid:state_pressed="true"android:drawable="@drawable/bg_selected">

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論