欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

RelativeLayout(相對(duì)布局)用法實(shí)例講解

 更新時(shí)間:2019年02月21日 11:14:10   投稿:laozhang  
在本文里小編給大家分享了關(guān)于RelativeLayout(相對(duì)布局)用法知識(shí)點(diǎn)以及對(duì)應(yīng)的實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。

本節(jié)引言

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

核心屬性圖

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

3.根據(jù)兄弟組件定位

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

圖中的組件1,2就是兄弟組件了,而組件3與組件1或組件2并不是兄弟組件,所以組件3不能通過(guò)組件1或2來(lái)進(jìn)行定位,比如layout_toleftof = "組件1"這樣是會(huì)報(bào)錯(cuò)的!切記!關(guān)于這個(gè)兄弟組件定位的最經(jīng)典例子就是"梅花布局"了,下面代碼實(shí)現(xiàn)下:

運(yùn)行效果圖:

實(shí)現(xiàn)代碼:

<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" >  
  
  <!-- 這個(gè)是在容器中央的 -->  
    
  <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的區(qū)別

初學(xué)者對(duì)于這兩個(gè)屬性可能會(huì)有一點(diǎn)混淆,這里區(qū)分下:首先margin代表的是偏移,比如marginleft = "5dp"表示組件離容器左邊緣偏移5dp; 而padding代表的則是填充,而填充的對(duì)象針對(duì)的是組件中的元素,比如TextView中的文字比如為TextView設(shè)置paddingleft = "5dp",則是在組件里的元素的左邊填充5dp的空間! margin針對(duì)的是容器中的組件,而padding針對(duì)的是組件中的元素,要區(qū)分開(kāi)來(lái)!下面通過(guò)簡(jiǎn)單的代碼演示兩者的區(qū)別:

比較示例代碼如下:

<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> 

運(yùn)行效果圖比較:

5.很常用的一點(diǎn):margin可以設(shè)置為負(fù)數(shù)

相信很多朋友都不知道一點(diǎn)吧,平時(shí)我們?cè)O(shè)置margin的時(shí)候都習(xí)慣了是正數(shù)的, 其實(shí)是可以用負(fù)數(shù)的,下面寫(xiě)個(gè)簡(jiǎn)單的程序演示下吧,模擬進(jìn)入軟件后,彈出廣告頁(yè)面的,右上角的cancle按鈕的margin則是使用負(fù)數(shù)的!

效果圖如下:

此處輸入圖片的描述

貼出的廣告Activity的布局代碼吧,當(dāng)然,如果你對(duì)這個(gè)有興趣的話可以下下demo, 因?yàn)閮H僅是實(shí)現(xiàn)效果,所以代碼會(huì)有些粗糙!

<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> 

相關(guān)文章

  • Android使用onCreateOptionsMenu()創(chuàng)建菜單Menu的方法詳解

    Android使用onCreateOptionsMenu()創(chuàng)建菜單Menu的方法詳解

    這篇文章主要介紹了Android使用onCreateOptionsMenu()創(chuàng)建菜單Menu的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android基于onCreateOptionsMenu創(chuàng)建菜單的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2016-11-11
  • Android短信發(fā)送器實(shí)現(xiàn)方法

    Android短信發(fā)送器實(shí)現(xiàn)方法

    這篇文章主要介紹了Android短信發(fā)送器實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了Android短信發(fā)送器從界面布局到功能實(shí)現(xiàn)的完整步驟與相關(guān)技巧,需要的朋友可以參考下
    2015-09-09
  • 詳解Android 視頻播放時(shí)停止后臺(tái)運(yùn)行的方法

    詳解Android 視頻播放時(shí)停止后臺(tái)運(yùn)行的方法

    這篇文章主要介紹了詳解Android 視頻播放時(shí)停止后臺(tái)運(yùn)行的方法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android Glide的簡(jiǎn)單使用

    Android Glide的簡(jiǎn)單使用

    本文主要介紹了Glide簡(jiǎn)單使用。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • Android 圖片處理縮放功能

    Android 圖片處理縮放功能

    這篇文章主要介紹了Android 圖片處理縮放功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • Android運(yùn)用BroadcastReceiver實(shí)現(xiàn)強(qiáng)制下線

    Android運(yùn)用BroadcastReceiver實(shí)現(xiàn)強(qiáng)制下線

    本篇文章主要介紹了Android運(yùn)用BroadcastReceiver實(shí)現(xiàn)強(qiáng)制下線,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android Dialog對(duì)話框用法實(shí)例詳解

    Android Dialog對(duì)話框用法實(shí)例詳解

    這篇文章主要介紹了Android Dialog對(duì)話框用法,結(jié)合實(shí)例形式分析了Android使用Dialog對(duì)話框過(guò)程中所涉及的創(chuàng)建、保存、回復(fù)等操作相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2016-07-07
  • Android kotlin+協(xié)程+Room數(shù)據(jù)庫(kù)的簡(jiǎn)單使用

    Android kotlin+協(xié)程+Room數(shù)據(jù)庫(kù)的簡(jiǎn)單使用

    這篇文章主要介紹了Android kotlin+協(xié)程+Room數(shù)據(jù)庫(kù)的簡(jiǎn)單使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕

    Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 一些有效的Android啟動(dòng)優(yōu)化策略分享

    一些有效的Android啟動(dòng)優(yōu)化策略分享

    在當(dāng)今激烈競(jìng)爭(zhēng)的移動(dòng)應(yīng)用市場(chǎng),應(yīng)用的啟動(dòng)速度直接影響著用戶的第一印象和滿意度,Android的啟動(dòng)優(yōu)化是開(kāi)發(fā)者必須關(guān)注的關(guān)鍵領(lǐng)域,本文將詳細(xì)介紹一些強(qiáng)大有效的Android啟動(dòng)優(yōu)化策略,幫助你優(yōu)化應(yīng)用的啟動(dòng)過(guò)程,為用戶創(chuàng)造更出色的體驗(yàn),需要的朋友可以參考下
    2023-08-08

最新評(píng)論