• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統(tǒng)計(jì)

            最新評(píng)論

            布局技巧:創(chuàng)建高效布局 --- 轉(zhuǎn)

            Android UI工具包提供了一些布局管理器,它們使用起來相當(dāng)容易,而且,大多數(shù)的時(shí)候,你只需要使用它們最基本的特征來實(shí)現(xiàn)UI。

            執(zhí)著于基本特征的使用對(duì)于創(chuàng)建UI來說,往往不是最高效的。一個(gè)常見的例子就是濫用LinearLayout,它將會(huì)導(dǎo)致View樹中的View數(shù)量激增。View——更糟的是,布局管理器——添加到應(yīng)用程序里都會(huì)帶來一定的消耗:初始化,布局和繪制變得更加緩慢。嵌套布局的花銷尤其“昂貴”,例如,如果你嵌套了一些LinearLayout,并使用了weight參數(shù),這會(huì)導(dǎo)致子元素要計(jì)算兩次。

            讓我們看一個(gè)非常簡(jiǎn)單且常見的布局例子:一個(gè)列表項(xiàng),左邊是一個(gè)圖標(biāo),右邊是標(biāo)題和描述,上方是標(biāo)題,下方是可選的描述。列表項(xiàng)可能看起來如下圖:

            為了清楚地認(rèn)識(shí)View之間(一個(gè)ImageView和兩個(gè)TextView)的相對(duì)位置,下圖是使用HierarchyViewer抓獲的布局剪影:

            實(shí)現(xiàn)這個(gè)布局,直接使用LinearLayout就可以了。列表項(xiàng)本身是一個(gè)水平的LinearLayout,里面有一個(gè)ImageView和一個(gè)垂直的LinearLayout,垂直的LinearLayout里包含兩個(gè)TextView。以下是這個(gè)布局的源代碼:

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="?android:attr/listPreferredItemHeight"
                android:padding="6dip">
                <ImageView
                    android:id="@+id/icon"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_marginRight="6dip"
                    android:src="@drawable/icon" />
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="0dip"
                    android:layout_weight="1"
                    android:layout_height="fill_parent">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="0dip"
                        android:layout_weight="1"
                        android:gravity="center_vertical"
                        android:text="My Application" />
                    <TextView  
                        android:layout_width="fill_parent"
                        android:layout_height="0dip"
                        android:layout_weight="1" 
                        android:singleLine="true"
                        android:ellipsize="marquee"
                        android:text="Simple application that shows how to use RelativeLayout" />
                    </LinearLayout>
            </LinearLayout>

            如果你將它作為ListViewitem,它能正常工作,但卻是相當(dāng)浪費(fèi)的。相同的布局可以使用RelativeLayout進(jìn)行重寫,相對(duì)于每個(gè)列表項(xiàng)來說,可以節(jié)省一個(gè)View,且View層級(jí)上更好,只有一層。使用RelativeLayout也很簡(jiǎn)單:

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="?android:attr/listPreferredItemHeight"
                android:padding="6dip">
                <ImageView
                    android:id="@+id/icon"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginRight="6dip"
                    android:src="@drawable/icon" />
            <TextView  
                    android:id="@+id/secondLine"
                    android:layout_width="fill_parent"
                    android:layout_height="26dip" 
                    android:layout_toRightOf="@id/icon"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:singleLine="true"
                    android:ellipsize="marquee"
                    android:text="Simple application that shows how to use RelativeLayout" />
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/icon"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_above="@id/secondLine"
                    android:layout_alignWithParentIfMissing="true"
                    android:gravity="center_vertical"
                    android:text="My Application" />
            </RelativeLayout>

            新的實(shí)現(xiàn)與老的實(shí)現(xiàn)看起來效果完全一致,除了一種情況。每個(gè)列表項(xiàng)顯示兩行文字:標(biāo)題和可選的描述。當(dāng)某一個(gè)列表項(xiàng)的描述不可獲得時(shí),應(yīng)用程序可能希望將第二個(gè)TextViewVisibility設(shè)為GONE。LinearLayout實(shí)現(xiàn)版表現(xiàn)得很完美,但RelativeLayout實(shí)現(xiàn)版就有點(diǎn)差強(qiáng)人意了:

            RelativeLayout里,每個(gè)View都是和父元素RelativeLayout對(duì)齊或是和其它View對(duì)齊的。例如,我們聲明描述部分是和RelativeLayout的底部對(duì)齊,標(biāo)題位于其上并與RelativeLayout的頂端對(duì)齊。當(dāng)描述GONE時(shí),RelativeLayout不知道怎么去放置標(biāo)題的底邊緣。為了解決這個(gè)問題,你可以使用一個(gè)非常簡(jiǎn)單的布局參數(shù):layout_alignWithParentIfMissing。

            這個(gè)布爾參數(shù)告訴RelativeLayout:如果目標(biāo)對(duì)象消失時(shí)使用自己的邊緣作為錨點(diǎn)。例如,如果你放置一個(gè)View到另一個(gè)Visibiity屬性設(shè)為GONEView的右邊,且設(shè)定alignWithParentIfMissingtrue,RelativeLayout就會(huì)將其左邊緣作為View的對(duì)齊錨點(diǎn)。在我們的這個(gè)場(chǎng)合,使用alignWithParentIfMissing的結(jié)果是RelativeLayout將標(biāo)題部分的底部與自己的底部對(duì)齊。結(jié)果如下所示:

            現(xiàn)在,我們的布局表現(xiàn)得很完美了,即使描述部分的Visibility屬性設(shè)為GONE。更好的是,層級(jí)更加簡(jiǎn)單,因?yàn)槲覀儾辉偈褂?/font>LinearLayout,而且,更加高效了。當(dāng)我們使用HierarchyViewer來比較兩個(gè)實(shí)現(xiàn)版的時(shí)候,事實(shí)就更明顯了:

            另外,當(dāng)你使用這么一個(gè)布局作為ListView的列表項(xiàng)時(shí),這種差異就更為重要了。希望這個(gè)簡(jiǎn)單的例子能讓你了解布局,了解如何優(yōu)化你的UI。

            posted on 2010-06-18 00:09 大龍 閱讀(551) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            色综合久久无码五十路人妻| 99久久国产主播综合精品| 久久中文精品无码中文字幕| 久久久久国产| 久久婷婷成人综合色综合| 久久综合久久久| 久久精品国产色蜜蜜麻豆| 久久精品视频网| 99精品久久久久久久婷婷| 亚洲欧美精品伊人久久| 97久久国产综合精品女不卡| 一本伊大人香蕉久久网手机| 久久人人爽人人爽人人爽| 国产精品成人久久久久三级午夜电影| 久久久久综合国产欧美一区二区| 久久精品国产精品亚洲毛片 | 久久精品国产亚洲AV无码麻豆| 欧美日韩中文字幕久久伊人| 亚洲精品无码久久久久去q| 久久亚洲国产成人精品无码区| 国产精品无码久久综合| 无码人妻久久一区二区三区免费 | 久久久久久毛片免费看| 99久久免费国产特黄| 久久精品国产亚洲av麻豆图片| 亚洲欧美国产日韩综合久久| 亚洲国产天堂久久综合网站| 久久精品国产99国产精品澳门| 乱亲女H秽乱长久久久| 亚洲午夜久久久久久久久电影网 | 91久久精品国产免费直播| 亚洲精品国产字幕久久不卡 | 综合网日日天干夜夜久久| 日韩中文久久| 久久中文字幕视频、最近更新| 久久久久这里只有精品 | 亚洲国产成人久久精品影视| 久久99国产精品久久99| 国产精品岛国久久久久| 伊人丁香狠狠色综合久久| 99久久精品这里只有精品|