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

Android布局技巧之合并布局

 更新時間:2016年06月01日 15:52:55   作者:xirihanlin  
這篇文章主要為大家詳細介紹了Android布局技巧之合并布局,感興趣的小伙伴們可以參考一下

我們已經有文章向你描述如何使用<include />標簽來重用和共享你的布局代碼。這篇文章將向你闡述<merge />標簽的使用以及如何與<include />標簽互補使用。

<merge />標簽用于減少View樹的層次來優(yōu)化Android的布局。通過看一個例子,你就能很容易的理解這個標簽能解決的問題。下面的XML布局顯示一個圖片,并且有一個標題位于其上方。這個結構相當?shù)暮唵?;FrameLayout里放置了一個ImageView,其上放置了一個TextView:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scaleType="center"
    android:src="@drawable/golden_gate" />
  <TextView
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="20dip"
    android:layout_gravity="center_horizontal|bottom"
    android:padding="12dip"
    android:background="#AA000000"
    android:textColor="#ffffffff"
    android:text="Golden Gate" />
</FrameLayout>

布局渲染起來很漂亮,而且看不出有什么問題:

當你使用HierarchyViewer工具來檢查時,你會發(fā)現(xiàn)事情變得很有趣。如果你仔細查看View樹,你將會注意到,我們在XML文件中定義的FrameLayout(藍色高亮顯示)是另一個FrameLayout唯一的子元素:


既然我們的FrameLayout和它的父元素有著相同的尺寸(歸功于fill_parent常量),并且也沒有定義任何的background,額外的padding或者gravity,所以它完全是無用的。我們所做的,只是讓UI變得更為復雜。怎樣我們才能擺脫這個FrameLayout呢?畢竟,XML文檔需要一個根標簽且XML布局總是與相應的View實例想對應。

這時候,<merge />標簽閃亮登場了。當LayoutInflater遇到這個標簽時,它會跳過它,并將<merge />內的元素添加到<merge />的父元素里。迷惑了嗎?讓我們用<merge />來替換FrameLayout,并重寫之前的XML布局:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
  <ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scaleType="center"
    android:src="@drawable/golden_gate" />
  <TextView
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="20dip"
    android:layout_gravity="center_horizontal|bottom"
    android:padding="12dip"
    android:background="#AA000000"
    android:textColor="#ffffffff"
    android:text="Golden Gate" />
</merge>

新的代碼中,TextView和ImageView都直接添加到上一層的FrameLayout里。雖然視覺上看起來一樣,但View的層次更加簡單了:


很顯然,在這個場合使用<merge />是因為Activity的ContentView的父元素始終是FrameLayout。如果你的布局使用LinearLayout作為它的根標簽(舉例),那么你就不能使用這個技巧。<merge />在其它的一些場合也很有用的。例如,它與<include />標簽結合起來就能表現(xiàn)得很完美。你還可以在創(chuàng)建一個自定義的組合View時使用<merge />。讓我們看一個使用<merge />創(chuàng)建一個新View的例子——OkCancelBar,包含兩個按鈕,并可以設置按鈕標簽。下面的XML用于在一個圖片上顯示自定義的View:

<merge
  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">
  <ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scaleType="center"
    android:src="@drawable/golden_gate" />
  <com.example.android.merge.OkCancelBar
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom"
    android:paddingTop="8dip"
    android:gravity="center_horizontal"
    android:background="#AA000000"
    okCancelBar:okLabel="Save"
    okCancelBar:cancelLabel="Don't save" />
</merge>

新的布局效果如下圖所示:


OkCancelBar的代碼很簡單,因為這兩個按鈕在外部的XML文件中定義,通過LayoutInflate類導入。如下面的代碼片段所示,R.layout.okcancelbar以OkCancelBar為父元素:

public class OkCancelBar extends LinearLayout {
  public OkCancelBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOrientation(HORIZONTAL);
    setGravity(Gravity.CENTER);
    setWeightSum(1.0f);
    
    LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
    
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);
    
    String text = array.getString(R.styleable.OkCancelBar_okLabel);
    if (text == null) text = "Ok";
    ((Button) findViewById(R.id.okcancelbar_ok)).setText(text);
    
    text = array.getString(R.styleable.OkCancelBar_cancelLabel);
    if (text == null) text = "Cancel";
    ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
    
    array.recycle();
  }
}

兩個按鈕的定義如下面的XML所示。正如你所看到的,我們使用<merge />標簽直接添加兩個按鈕到OkCancelBar。每個按鈕都是從外部相同的XML布局文件包含進來的,便于維護;我們只是簡單地重寫它們的id:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
  <include
    layout="@layout/okcancelbar_button"
    android:id="@+id/okcancelbar_ok" />    
  <include
    layout="@layout/okcancelbar_button"
    android:id="@+id/okcancelbar_cancel" />
</merge>

我們創(chuàng)建了一個靈活且易于維護的自定義View,它有著高效的View層次:

<merge />標簽極其有用。然而它也有以下兩個限制:
·         <merge />只能作為XML布局的根標簽使用
·         當Inflate以<merge />開頭的布局文件時,必須指定一個父ViewGroup,并且必須設定attachToRoot為true(參看inflate(int, android.view.ViewGroup, Boolean)方法)。 

效果圖:


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android移除Message的方法分享

    Android移除Message的方法分享

    本篇文章主要介紹MessageQueue提供的各種移除Message的方法,大概有八九個,接下來會對其中比較典型的移除方法進行詳細分析,需要的可以參考一下
    2022-10-10
  • Android實現(xiàn)底部圖片選擇Dialog

    Android實現(xiàn)底部圖片選擇Dialog

    這篇文章主要為大家詳細介紹了Android實現(xiàn)底部圖片選擇Dialog,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android學習筆記45之gson解析json

    Android學習筆記45之gson解析json

    JSON即JavaScript Object Natation,是一種輕量級的數(shù)據交換格式,采用完全獨立于語言的文本格式,為Web開發(fā)提供了一種理想的數(shù)據交換格式。通過本篇文章給大家介紹Android學習筆記45之gson解析json的相關內容,對android gson解析json相關知識感興趣的朋友一起學習吧
    2015-12-12
  • Android仿QQ復制昵稱效果的實現(xiàn)方法

    Android仿QQ復制昵稱效果的實現(xiàn)方法

    這篇文章主要介紹了Android仿QQ復制昵稱效果的實現(xiàn)方法,主要依賴的是一個開源項目,需要的朋友可以參考下
    2019-05-05
  • Android實現(xiàn)計步傳感器功能

    Android實現(xiàn)計步傳感器功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)計步傳感器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android中把bitmap存成BMP格式圖片的方法

    Android中把bitmap存成BMP格式圖片的方法

    這篇文章主要介紹了Android中把bitmap存成BMP格式圖片的方法,需要的朋友可以參考下
    2016-01-01
  • Android入門教程之Fragment的具體使用詳解

    Android入門教程之Fragment的具體使用詳解

    Fragment是Android3.0后引入的一個新的API,他出現(xiàn)的初衷是為了適應大屏幕的平板電腦, 當然現(xiàn)在他仍然是平板APP UI設計的寵兒,而且我們普通手機開發(fā)也會加入這個Fragment, 我們可以把他看成一個小型的Activity,又稱Activity片段
    2021-10-10
  • Android四大組件之Service服務詳細講解

    Android四大組件之Service服務詳細講解

    Android的服務是開發(fā)Android應用程序的重要組成部分。不同于活動Activity,服務是在后臺運行,服務沒有接口,生命周期也與活動Activity非常不同。通過使用服務我們可以實現(xiàn)一些后臺操作,比如想從遠程服務器加載一個網頁等,下面來看看詳細內容,需要的朋友可以參考下
    2022-07-07
  • Android實現(xiàn)手寫簽名

    Android實現(xiàn)手寫簽名

    這篇文章主要為大家詳細介紹了Android實現(xiàn)手寫簽名的具體實現(xiàn)方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • OkHttp原理分析小結

    OkHttp原理分析小結

    OkHttp 是 Square 公司開源的一款網絡框架,封裝了一個高性能的 http 請求庫,本文對OkHttp原理給大家詳細講解,感興趣的朋友跟隨小編一起看看吧
    2024-01-01

最新評論

台东县| 铜陵市| 海林市| 池州市| 寻甸| 商水县| 寿阳县| 晋宁县| 龙泉市| 佛坪县| 山东省| 棋牌| 浦县| 武安市| 宜阳县| 邵阳市| 松潘县| 玛纳斯县| 怀远县| 北海市| 瑞丽市| 三原县| 南通市| 建阳市| 尤溪县| 德化县| 闽清县| 昔阳县| 商洛市| 青川县| 重庆市| 民县| 溧阳市| 沙雅县| 东源县| 孝昌县| 江源县| 西乌珠穆沁旗| 达州市| 永泰县| 盐池县|