<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                #### 15.1.1 布局優化 布局優化的思想很簡單,就是盡量減少布局文件的層級,這個道理是很淺顯的,布局中的層級少了,這就意味著Android繪制時的工作量少了,那么程序的性能自然就高了。 如何進行布局優化呢?首先刪除布局中無用的控件和層級,其次有選擇地使用性能較低的ViewGroup,比如RelativeLayout。如果布局中既可以使用LinearLayout也可以使用RelativeLayout,那么就采用LinearLayout,這是因為RelativeLayout的功能比較復雜,它的布局過程需要花費更多的CPU時間。FrameLayout和LinearLayout一樣都是一種簡單高效的ViewGroup,因此可以考慮使用它們,但是很多時候單純通過一個LinearLayout或者FrameLayout無法實現產品效果,需要通過嵌套的方式來完成。這種情況下還是建議采用RelativeLayout,因為ViewGroup的嵌套就相當于增加了布局的層級,同樣會降低程序的性能。 布局優化的另外一種手段是采用`<include>`標簽、`<merge>`標簽和ViewStub。`<include>`標簽主要用于布局重用,`<merge>`標簽一般和`<include>`配合使用,它可以降低減少布局的層級,而ViewStub則提供了按需加載的功能,當需要時才會將ViewStub中的布局加載到內存,這提高了程序的初始化效率,下面分別介紹它們的使用方法。 `<include>`標簽 `<include>`標簽可以將一個指定的布局文件加載到當前的布局文件中,如下所示。 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/app_bg" android:gravity="center_horizontal"> <include layout="@layout/titlebar"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text" android:padding="5dp" /> ... </LinearLayout> 上面的代碼中,@layout/titlebar指定了另外一個布局文件,通過這種方式就不用把titlebar這個布局文件的內容再重復寫一遍了,這就是`<include>`的好處。`<include>`標簽只支持以android:layout_開頭的屬性,比如android:layout_width、android:layout_height,其他屬性是不支持的,比如android:background。當然,android:id這個屬性是個特例,如果<include>指定了這個id屬性,同時被包含的布局文件的根元素也指定了id屬性,那么以<include>指定的id屬性為準。需要注意的是,如果<include>標簽指定了android:layout_*這種屬性,那么要求android:layout_width和android:layout_height必須存在,否則其他android:layout_*形式的屬性無法生效,下面是一個指定了android:layout_*屬性的示例。 <include android:id="@+id/new_title" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/title"/> `<merge>`標簽 `<merge>`標簽一般和`<include>`標簽一起使用從而減少布局的層級。在上面的示例中,由于當前布局是一個豎直方向的LinearLayout,這個時候如果被包含的布局文件中也采用了豎直方向的LinearLayout,那么顯然被包含的布局文件中的LinearLayout是多余的,通過`<merge>`標簽就可以去掉多余的那一層LinearLayout,如下所示。 <merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/one"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/two"/> </merge> `ViewStub ` ViewStub繼承了View,它非常輕量級且寬/高都是0,因此它本身不參與任何的布局和繪制過程。ViewStub的意義在于按需加載所需的布局文件,在實際開發中,有很多布局文件在正常情況下不會顯示,比如網絡異常時的界面,這個時候就沒有必要在整個界面初始化的時候將其加載進來,通過ViewStub就可以做到在使用的時候再加載,提高了程序初始化時的性能。下面是一個ViewStub的示例: <ViewStub android:id="@+id/stub_import" android:inflatedId="@+id/panel_import" android:layout="@layout/layout_network_error" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /> 其中stub_import是ViewStub的id,而panel_import是layout/layout_network_error這個布局的根元素的id。如何做到按需加載呢?在需要加載ViewStub中的布局時,可以按照如下兩種方式進行: ((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); 或者 View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); 當ViewStub通過setVisibility或者inflate方法加載后,ViewStub就會被它內部的布局替換掉,這個時候ViewStub就不再是整個布局結構中的一部分了。另外,目前ViewStub還不支持<merge>標簽。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看