麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

RelativeLayout(相對布局)用法實例講解

2019-10-21 21:25:13
字體:
來源:轉載
供稿:網友

本節引言

LinearLayout也是我們用的比較多的一個布局,我們更多的時候更鐘情于他的weight(權重)屬性,等比例劃分,對屏幕適配還是幫助蠻大的;但是使用LinearLayout的時候也有一個問題,就是當界面比較復雜的時候,需要嵌套多層的 LinearLayout,這樣就會降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的 item,效率會更低,另外太多層LinearLayout嵌套會占用更多的系統資源,還有可能引發stackoverflow; 但是如果我們使用RelativeLayout的話,可能僅僅需要一層就可以完成了,以父容器或者兄弟組件參考+margin +padding就可以設置組件的顯示位置,是比較方便的!當然,也不是絕對的,具體問題具體分析吧! 總結就是:盡量使用RelativeLayout + LinearLayout的weight屬性搭配使用吧!

核心屬性圖

RelativeLayout,相對布局

2.父容器定位屬性示意圖

RelativeLayout,相對布局

3.根據兄弟組件定位

恩,先說下什么是兄弟組件吧,所謂的兄弟組件就是處于同一層次容器的組件,如圖

RelativeLayout,相對布局

圖中的組件1,2就是兄弟組件了,而組件3與組件1或組件2并不是兄弟組件,所以組件3不能通過組件1或2來進行定位,比如layout_toleftof = "組件1"這樣是會報錯的!切記!關于這個兄弟組件定位的最經典例子就是"梅花布局"了,下面代碼實現下:

運行效果圖:

RelativeLayout,相對布局

實現代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/RelativeLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent" >      <!-- 這個是在容器中央的 -->        <ImageView      android:id="@+id/img1"       android:layout_width="80dp"      android:layout_height="80dp"      android:layout_centerInParent="true"      android:src="@drawable/pic1"/>        <!-- 在中間圖片的左邊 -->    <ImageView      android:id="@+id/img2"       android:layout_width="80dp"      android:layout_height="80dp"      android:layout_toLeftOf="@id/img1"      android:layout_centerVertical="true"      android:src="@drawable/pic2"/>        <!-- 在中間圖片的右邊 -->    <ImageView      android:id="@+id/img3"       android:layout_width="80dp"      android:layout_height="80dp"      android:layout_toRightOf="@id/img1"      android:layout_centerVertical="true"      android:src="@drawable/pic3"/>        <!-- 在中間圖片的上面-->    <ImageView      android:id="@+id/img4"       android:layout_width="80dp"      android:layout_height="80dp"      android:layout_above="@id/img1"      android:layout_centerHorizontal="true"      android:src="@drawable/pic4"/>        <!-- 在中間圖片的下面 -->    <ImageView      android:id="@+id/img5"       android:layout_width="80dp"      android:layout_height="80dp"      android:layout_below="@id/img1"      android:layout_centerHorizontal="true"      android:src="@drawable/pic5"/>    </RelativeLayout>

4.margin與padding的區別

初學者對于這兩個屬性可能會有一點混淆,這里區分下:首先margin代表的是偏移,比如marginleft = "5dp"表示組件離容器左邊緣偏移5dp; 而padding代表的則是填充,而填充的對象針對的是組件中的元素,比如TextView中的文字比如為TextView設置paddingleft = "5dp",則是在組件里的元素的左邊填充5dp的空間! margin針對的是容器中的組件,而padding針對的是組件中的元素,要區分開來!下面通過簡單的代碼演示兩者的區別:

比較示例代碼如下:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >      <Button      android:id="@+id/btn1"       android:layout_height="wrap_content"      android:layout_width="wrap_content"      android:text="Button"/>    <Button      android:paddingLeft="100dp"       android:layout_height="wrap_content"      android:layout_width="wrap_content"      android:text="Button"      android:layout_toRightOf="@id/btn1"/>        <Button      android:id="@+id/btn2"       android:layout_height="wrap_content"      android:layout_width="wrap_content"      android:text="Button"      android:layout_alignParentBottom="true"/>    <Button      android:layout_marginLeft="100dp"       android:layout_height="wrap_content"      android:layout_width="wrap_content"      android:text="Button"      android:layout_toRightOf="@id/btn2"       android:layout_alignParentBottom="true"/>      </RelativeLayout> 

運行效果圖比較:

RelativeLayout,相對布局

5.很常用的一點:margin可以設置為負數

相信很多朋友都不知道一點吧,平時我們設置margin的時候都習慣了是正數的, 其實是可以用負數的,下面寫個簡單的程序演示下吧,模擬進入軟件后,彈出廣告頁面的,右上角的cancle按鈕的margin則是使用負數的!

效果圖如下:

RelativeLayout,相對布局

貼出的廣告Activity的布局代碼吧,當然,如果你對這個有興趣的話可以下下demo, 因為僅僅是實現效果,所以代碼會有些粗糙!

<RelativeLayout 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"   tools:context="com.jay.example.relativelayoutdemo.MainActivity"    android:background="#00CCCCFF">    <ImageView     android:id="@+id/imgBack"     android:layout_width="200dp"     android:layout_height="200dp"     android:layout_centerInParent="true"     android:background="@drawable/myicon" />    <ImageView     android:id="@+id/imgCancle"     android:layout_width="28dp"     android:layout_height="28dp"     android:layout_alignRight="@id/imgBack"     android:layout_alignTop="@id/imgBack"     android:background="@drawable/cancel"     android:layout_marginTop="-15dp"     android:layout_marginRight="-10dp" />  </RelativeLayout> 
 


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 免费观看的毛片手机视频 | 国产精品99久久久久久久 | 久久精品中文 | 大学生a级毛片免费视频 | 九九热九九爱 | 久久久一区二区三区四区 | 国产精品99久久久久久大便 | 亚洲一区二区三区视频免费 | 91一级毛片 | 在线免费观看毛片 | 最新欧美精品一区二区三区 | 最新一区二区三区 | 久久成人国产精品入口 | 中文字幕一二区 | 欧美日韩大片在线观看 | 好吊色欧美一区二区三区四区 | 成人午夜视频免费在线观看 | 精品成人av一区二区三区 | 欧美在线a | 黄色成人在线 | 免费在线看黄 | 在火车上摸两乳爽的大叫 | 久久亚洲精选 | 色av综合在线 | 7777在线观看 | 亚洲国产成人久久成人52 | 免费一级片观看 | 欧美三级美国一级 | 竹内纱里奈和大战黑人 | 日韩精品中文字幕一区 | 精国产品一区二区三区四季综 | 国产99一区二区 | 免费毛片电影 | 国产福利不卡一区二区三区 | 精品国产91久久久 | 黄色网址免费在线 | 九九视屏 | 国产亚洲精品久久久久5区 综合激情网 | 一级黄色淫片 | 婷婷久久青草热一区二区 | 久久精品国产久精国产 |