最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程之絕對布局AbsoluteLayout和相對布局RelativeLayout實例詳解

 更新時間:2015年12月22日 14:40:34   作者:Android開發(fā)網(wǎng)  
這篇文章主要介紹了Android編程之絕對布局AbsoluteLayout和相對布局RelativeLayout實現(xiàn)方法,結(jié)合實例形式詳細分析了Android絕對布局AbsoluteLayout和相對布局RelativeLayout的原理與使用技巧,需要的朋友可以參考下

本文實例分析了Android編程之絕對布局AbsoluteLayout和相對布局RelativeLayout。分享給大家供大家參考,具體如下:

 一、絕對布局AbsoluteLayout

絕對定位AbsoluteLayout,又可以叫做坐標布局,可以直接指定子元素的絕對位置,這種布局簡單直接,直觀性強,但是由于手機屏幕尺寸差別比較大,使用絕對定位的適應(yīng)性會比較差。

下面我們舉一個例子看看:例子里的機器人圖片大小是250X250,可以看到我們使用android:layout_x和android:layout_y來指定子元素的縱橫坐標。

XML/HTML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout android:id="@+id/AbsoluteLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="#fff"><ImageView 
android:src="@drawable/android" 
android:layout_y="40dip" 
android:layout_width="wrap_content" 
android:layout_x="35dip" 
android:id="@+id/ImageView01" 
android:layout_height="wrap_content"> 
</ImageView> 
<TextView 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:id="@+id/TextView01" 
android:text="Android2.2 學(xué)習(xí)指南" 
android:textColor="#0f0" 
android:textSize="28dip" 
android:layout_y="330dip" 
android:layout_x="35dip“> 
</TextView> 
<TextView 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:id="@+id/TextView02" 
android:text="圖文并茂,理論清晰,操作性強" 
android:textColor="#333" 
android:textSize="18dip" 
android:layout_y="365dip" 
android:layout_x="35dip“> 
</TextView> 
</AbsoluteLayout>

讓我們看一下在WQVGA的模擬器下的顯示效果:

再在WVGA800的模擬器下看看顯示效果:

Tip: 在絕對定位中,如果子元素不設(shè)置layout_x和layout_y,那么它們的默認值是0,也就是說它會像在FrameLayout一樣這個元素會出現(xiàn)在左上角。

二、相對布局RelativeLayout

相對布局 RelativeLayout 允許子元素指定它們相對于其父元素或兄弟元素的位置,這是實際布局中最常用的布局方式之一。它靈活性大很多,當(dāng)然屬性也多,操作難度也大,屬性之間產(chǎn)生沖突的的可能性也大,使用相對布局時要多做些測試。

下面我們用相對布局再做一次上面的例子,首先放置一個圖片,其它兩個文本分別相對上一個元素定位:

XML/HTML代碼:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#fff" 
xmlns:android="http://schemas.android.com/apk/res/android"><ImageView android:id="@+id/ImageView01" 
android:src="@drawable/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="40dip" 
> 
</ImageView> 
<TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView01" 
android:text="Android2.2 學(xué)習(xí)指南" 
android:textColor="#0f0" 
android:textSize="28dip" 
android:layout_below="@id/ImageView01" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="10dip"> 
</TextView> 
<TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView02" 
android:text="圖文并茂,理論清晰,操作性強" 
android:textColor="#333" 
android:textSize="18dip" 
android:layout_below="@id/TextView01" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="5dip“> 
</TextView> 
</RelativeLayout> 

讓我們看一下在WQVGA的模擬器下的顯示效果:

再看一下在更大屏幕(WVGA800)模擬器上的顯示效果:

從上圖可以看到界面效果基本保持了一致,而不是像絕對定位一樣龜縮在左上角;同學(xué)們看到自動縮放的功能是采用了dip做單位帶來的好處。

下面介紹一下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                            如果對應(yīng)的兄弟元素找不到的話就以父元素做參照物
第二類:屬性值必須為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                   離某元素上邊緣的距離

我們再把上面的例子重新做一遍,這一次多放一些屬性在里面,大家試驗一下:

XML/HTML代碼:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#cfff" 色彩的設(shè)置是argb,第一個c是透明度 
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/ImageView01" 
android:src="@drawable/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginTop="40dip" 
android:layout_centerHorizontal="true"> 
</ImageView><TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView01" 
android:text="Android2.2 學(xué)習(xí)指南" 
android:textColor="#0f0" 
android:textSize="28dip" 
android:layout_below="@id/ImageView01" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="10dip"> 
</TextView><TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView02" 
android:text="圖文并茂,理論清晰,操作性強" 
android:textColor="#333" 
android:textSize="18dip" 
android:layout_below="@id/TextView01" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="5dip"> 
</TextView>
<TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView03" 
android:text="alignTop" 
android:textColor="#333" 
android:textSize="18dip" 
android:layout_alignTop="@id/ImageView01" 和ImageView01上邊緣對齊 
android:layout_centerHorizontal="true"> 
</TextView><TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView04" 
android:text="alignLeft" 
android:textColor="#333" 
android:textSize="18dip" 
android:layout_alignLeft="@id/ImageView01" 
android:layout_centerHorizontal="true"> 
</TextView><TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView05" 
android:text="alignRight" 
android:textColor="#333" 
android:textSize="18dip" 
android:layout_alignRight="@id/ImageView01" 
android:layout_centerHorizontal="true"> 
</TextView><TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView06" 
android:text="alignBottom" 
android:textColor="#333" 
android:textSize="18dip" 
android:layout_alignBottom="@id/ImageView01" 
android:layout_centerHorizontal="true"> 
</TextView> 
</RelativeLayout> 

絕對布局AbsoluteLayout和相對布局RelativeLayout的內(nèi)容就講完了,希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android iconify 使用詳解

    Android iconify 使用詳解

    iconify是一個矢量圖標庫,包含使用 Dave Gandy 制作的超過370中矢量字體圖標,可以使Android應(yīng)用開發(fā)者免于制作多種適用于不同屏幕大小尺寸的圖片,從而提高開發(fā)者工作效率。下文重點給大家介紹android iconify 使用,感興趣的朋友一起學(xué)習(xí)吧
    2017-08-08
  • Android Button點擊事件的四種實現(xiàn)方法

    Android Button點擊事件的四種實現(xiàn)方法

    這篇文章主要為大家詳細介紹了Android Button點擊事件的四種實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android自定義滑動接聽電話控件組實例

    Android自定義滑動接聽電話控件組實例

    這篇文章主要介紹了Android自定義滑動接聽電話控件組,接聽電話可以左右滑動,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • Android入門之實現(xiàn)自定義Adapter

    Android入門之實現(xiàn)自定義Adapter

    這篇文章主要為大家詳細介紹了Android如何實現(xiàn)自定義Adapter,文中的示例代碼講解詳細,對我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下
    2022-11-11
  • Android之AttributeSet案例詳解

    Android之AttributeSet案例詳解

    這篇文章主要介紹了Android之AttributeSet案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android中使用TextureView播放視頻

    Android中使用TextureView播放視頻

    Android使用SurfaceView播放視頻不能使用變換,本文教你如何在Android中使用TextureView播放視頻平移、縮放、旋轉(zhuǎn)設(shè)置透明度
    2018-04-04
  • Android中WebView的基本配置與填坑記錄大全

    Android中WebView的基本配置與填坑記錄大全

    webview是一直都很痛恨的控件,你又不能不用,但是一旦大規(guī)模測試起來你就發(fā)現(xiàn)這個webview真是坑。各種你想不到的錯誤 在各種奇怪的手機,各種不一樣的版本里出現(xiàn)各種想不到的問題。本文就介紹了Android中WebView的基本配置與遇到的一些填坑記錄,需要的朋友可以參考下。
    2017-11-11
  • Android 個人理財工具二:使用SQLite實現(xiàn)啟動時初始化數(shù)據(jù)

    Android 個人理財工具二:使用SQLite實現(xiàn)啟動時初始化數(shù)據(jù)

    本文主要介紹 Android 使用SQLite實現(xiàn)啟動時初始化數(shù)據(jù),這里對SQLite 的數(shù)據(jù)庫進行詳解,附有示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • Android仿微信錄音功能(錄音后的raw文件轉(zhuǎn)mp3文件)

    Android仿微信錄音功能(錄音后的raw文件轉(zhuǎn)mp3文件)

    這篇文章主要介紹了Android中仿微信錄音功能錄音后的raw文件轉(zhuǎn)mp3文件,本文通過實例代碼給大家講解的非常詳細,需要的朋友可以參考下
    2019-11-11
  • Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法詳解

    Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法詳解

    這篇文章主要介紹了Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法,結(jié)合實例形式較為詳細的總結(jié)分析了Android WebView控件的功能、布局、設(shè)置、常用方法及相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10

最新評論

金阳县| 雷山县| 通榆县| 博乐市| 卢湾区| 凤山市| 门头沟区| 宁安市| 铅山县| 布拖县| 沧源| 扎兰屯市| 英山县| 玛曲县| 乐陵市| 通化市| 东方市| 庐江县| 怀安县| 永春县| 扶沟县| 都江堰市| 方正县| 宝鸡市| 宁国市| 安西县| 全椒县| 漳平市| 鹤峰县| 靖西县| 会理县| 砚山县| 全州县| 勐海县| 拉孜县| 榆林市| 富宁县| 宜城市| 临城县| 永寿县| 三明市|