android Activity線性布局和表格布局實(shí)例講解
實(shí)驗(yàn)中只需要編寫(xiě)相應(yīng)的xml的代碼,java代碼不需要更改,因?yàn)槲覀冞@里只是練習(xí)android的界面設(shè)計(jì)。
線性布局:
線性布局就是將各種控件按照行或者列依次進(jìn)行排列。
其中本實(shí)驗(yàn)用到的各控件的屬性解釋如下:
android:layout_weight屬性是指不同的控件在activity中占有體積大小的比例。
android:paddingLeft指內(nèi)邊距左的距離,即控件內(nèi)文字離控件左邊邊界的距離。其它的類(lèi)推。
android:gravity指控件內(nèi)文字相對(duì)于控件本身的方向?qū)傩?長(zhǎng)度為dip,與像素獨(dú)立的長(zhǎng)度。
android:background為控件內(nèi)文字顏色的背景色,顏色采用rgb時(shí)前面需用”#”號(hào).
android:textSize為文本的大小,單位為pt,即鎊。
android:id為該控件的id,即在此處可以設(shè)置控件的id。
android:layout_width為控件本身的寬度屬性,其它的類(lèi)似。
實(shí)驗(yàn)結(jié)果顯示2行字,分別設(shè)置了不同的屬性。
效果如下:
xml代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--
線性布局中
android:layout_weight屬性是指不同的控件在activity中占有體積大小的比例。
android:paddingLeft指內(nèi)邊距左的距離,即控件內(nèi)文字離控件左邊邊界的距離。其它的類(lèi)推。
android:gravity指控件內(nèi)文字相對(duì)于控件本身的方向?qū)傩?長(zhǎng)度為dip,與像素獨(dú)立的長(zhǎng)度。
android:background為控件內(nèi)文字顏色的背景色,顏色采用rgb時(shí)前面需用”#”號(hào).
android:textSize為文本的大小,單位為pt,即鎊。
android:id為該控件的id,即在此處可以設(shè)置控件的id。
android:layout_width為控件本身的寬度屬性,其它的類(lèi)似。
-->
<TextView
android:id="@+id/London"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="倫敦奧運(yùn)"
android:textSize="20pt"
android:background="#00ff00"
android:gravity="center_horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:layout_weight="1"
/>
<TextView
android:id="@+id/China"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="中國(guó)加油!??!"
android:textSize="35pt"
android:background="#ff0000"
android:layout_weight="3"
/>
</LinearLayout>
表格布局:
表格布局有點(diǎn)類(lèi)似表單的意思,可以在activity中建立多行,每一行又可以設(shè)置為多列,所以看起來(lái)橫豎條理比較清晰,因此叫做表格布局。
表格布局各控件屬性與線性布局類(lèi)似,本實(shí)驗(yàn)用到的屬性解釋如下:
用TableRow來(lái)增加一行,然后該行內(nèi)各列依次并排。
android:padding指的是內(nèi)邊距的4個(gè)方向都采用同樣的間距。
android:stretchColumns屬性表示當(dāng)該行屬性設(shè)置為填充屏幕時(shí),指定將哪一列拉伸。
實(shí)驗(yàn)結(jié)果為顯示2行,每一行又有4列。
效果如下:

xml代碼如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:text="國(guó)家"
android:background="#848484"
android:padding="2dip"
/>
<TextView
android:text="金牌"
android:background="#ff0000"
android:padding="2dip"
/>
<TextView
android:text="銀牌"
android:background="#00ff00"
android:padding="2dip"
/>
<TextView
android:text="銅牌"
android:background="#0000ff"
android:padding="2dip"
/>
</TableRow>
<TableRow >
<TextView
android:text="中國(guó)"
android:background="#848484"
android:padding="2dip"
/>
<TextView
android:text="*"
android:background="#ff0000"
android:padding="2dip"
/>
<TextView
android:text="**"
android:background="#00ff00"
android:padding="2dip"
/>
<TextView
android:text="***"
android:background="#0000ff"
android:padding="2dip"
/>
</TableRow>
<TableRow >
<TextView
android:text="美國(guó)"
android:background="#848484"
android:padding="2dip"
/>
<TextView
android:text="*"
android:background="#ff0000"
android:padding="2dip"
/>
<TextView
android:text="**"
android:background="#00ff00"
android:padding="2dip"
/>
<TextView
android:text="***"
android:background="#0000ff"
android:padding="2dip"
/>
</TableRow>
</TableLayout>
線性布局和表格布局混合:
混合布局原理類(lèi)似,只是大的layout中嵌入小layout,且小layout中又可以嵌入不同的layout。
這次實(shí)驗(yàn)將上面的2個(gè)實(shí)驗(yàn)混合起來(lái)顯示的,即總的布局為垂直方向上的線性布局,上面那個(gè)布局內(nèi)部又為垂直方向的布局,下面那個(gè)布局為也是一個(gè)線性布局,不過(guò)里面嵌入了一個(gè)表格布局,所以總共有4個(gè)布局。
效果如下:

xml代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/London"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="倫敦奧運(yùn)"
android:textSize="5pt"
android:background="#00ff00"
android:gravity="center_horizontal"
android:padding="10pt"
android:layout_weight="1"
/>
<TextView
android:id="@+id/China"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="中國(guó)加油?。?!"
android:textSize="8pt"
android:background="#ff00ff"
android:layout_weight="3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:text="國(guó)家"
android:background="#848484"
android:padding="2dip"
/>
<TextView
android:text="金牌"
android:background="#ff0000"
android:padding="2dip"
/>
<TextView
android:text="銀牌"
android:background="#00ff00"
android:padding="2dip"
/>
<TextView
android:text="銅牌"
android:background="#0000ff"
android:padding="2dip"
/>
</TableRow>
<TableRow >
<TextView
android:text="中國(guó)"
android:background="#848484"
android:padding="2dip"
/>
<TextView
android:text="*"
android:background="#ff0000"
android:padding="2dip"
/>
<TextView
android:text="**"
android:background="#00ff00"
android:padding="2dip"
/>
<TextView
android:text="***"
android:background="#0000ff"
android:padding="2dip"
/>
</TableRow>
<TableRow >
<TextView
android:text="美國(guó)"
android:background="#848484"
android:padding="2dip"
/>
<TextView
android:text="*"
android:background="#ff0000"
android:padding="2dip"
/>
<TextView
android:text="**"
android:background="#00ff00"
android:padding="2dip"
/>
<TextView
android:text="***"
android:background="#0000ff"
android:padding="2dip"
/>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
實(shí)驗(yàn)總結(jié):
通過(guò)本次實(shí)驗(yàn)對(duì)activity的簡(jiǎn)單布局有了個(gè)初步的了解。
作者:tornadomeet
相關(guān)文章
android實(shí)現(xiàn)桌面移動(dòng)懸浮窗口
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)桌面移動(dòng)懸浮窗口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android 使用AsyncTask實(shí)現(xiàn)斷點(diǎn)續(xù)傳
這篇文章主要介紹了Android 使用AsyncTask實(shí)現(xiàn)斷點(diǎn)續(xù)傳的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05詳解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅
這篇文章主要介紹了詳解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android 實(shí)現(xiàn)仿網(wǎng)絡(luò)直播彈幕功能詳解及實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)仿網(wǎng)絡(luò)直播彈幕功能詳解的相關(guān)資料,并附實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11直接可用的Android studio學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了直接可用的Android studio學(xué)生信息管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Android手機(jī)App安全漏洞整理(小結(jié))
這篇文章主要介紹了Android手機(jī)App安全漏洞整理(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Android使用OKHttp包處理HTTP相關(guān)操作的基本用法講解
這篇文章主要介紹了Android使用OKHttp包處理HTTP相關(guān)操作的基本用法講解,包括操作如何利用OKHttp操作HTTP請(qǐng)求與處理緩存等內(nèi)容,需要的朋友可以參考下2016-07-07Android實(shí)現(xiàn)仿慕課網(wǎng)下拉加載動(dòng)畫(huà)
這篇文章是我在做動(dòng)畫(huà)的項(xiàng)目中整理出來(lái)的,在eoe看了篇帖子,然后仿慕課網(wǎng)做了一個(gè)下拉加載動(dòng)畫(huà)。此功能實(shí)現(xiàn)方法是AnimationDrawable類(lèi)進(jìn)行 Animation-list中item的循環(huán)遍歷圖片,類(lèi)似于flash里的幀幀動(dòng)畫(huà),需要的朋友可以參考下2015-07-07