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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                > 編寫:[allenlsy](https://github.com/allenlsy) - 原文:[http://developer.android.com/training/improving-layouts/reusing-layouts.html](http://developer.android.com/training/improving-layouts/reusing-layouts.html) 雖然 Android 提供很多小的可重用的交互組件,你仍然可能需要重用復雜一點的組件,這也許會用到 Layout。為了高效重用整個的 Layout,你可以使用 `<include/>` 和 `<merge/>` 標簽把其他 Layout 嵌入當前 Layout。 重用 Layout 非常強大,它讓你可以創建復雜的可重用 Layout。比如,一個 yes/no 按鈕面板,或者帶有文字的自定義進度條。這也意味著,任何在多個 Layout 中重復出現的元素可以被提取出來,被單獨管理,再添加到 Layout 中。所以,雖然可以添加一個自定義 View 來實現單獨的 UI 組件,你可以更簡單的直接重用某個 Layout 文件。 ### 創建可重用 Layout 如果你已經知道你需要重用的 Layout,就先創建一個新的 XML 文件并定義 Layout 。比如,以下是一個來自 G-Kenya codelab 的 Layout,定義了一個需要添加到每個 [Activity](# "An activity represents a single screen with a user interface.") 中的標題欄(titlebar.xml): ~~~ <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=”match_parent” android:layout_height="wrap_content" android:background="@color/titlebar_bg"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gafricalogo" /> </FrameLayout> ~~~ 根節點 View 就是你想添加入的 Layout 類型。 ### 使用`<include>`標簽 使用 `<include>` 標簽,可以在 Layout 中添加可重用的組件。比如,這里有一個來自 G-Kenya codelab 的 Layout 需要包含上面的那個標題欄: ~~~ <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/hello" android:padding="10dp" /> ... </LinearLayout> ~~~ 你也可以覆寫被添加的 Layout 的所有 Layout 參數(任何 android:layout_* 屬性),通過在 `<include/>` 中聲明他們來完成。比如: ~~~ <include android:id="@+id/news_title" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/title"/> ~~~ 然而,如果你要在 `<include>` 中覆寫某些屬性,你必須先覆寫 `android:layout_height` 和 `android:layout_width`。 ### 使用`<merge>`標簽 `<merge />` 標簽在你嵌套 Layout 時取消了 UI 層級中冗余的 ViewGroup 。比如,如果你有一個 Layout 是一個豎直方向的 LinearLayout,其中包含兩個連續的 View 可以在別的 Layout 中重用,那么你會做一個 LinearLayout 來包含這兩個 View ,以便重用。不過,當使用一個 LinearLayout 作為另一個 LinearLayout 的根節點時,這種嵌套 LinearLayout 的方式除了減慢你的 UI 性能外沒有任何意義。 為了避免這種情況,你可以用 `<merge>` 元素來替代可重用 Layout 的根節點。例如: ~~~ <merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/delete"/> </merge> ~~~ 現在,當你要將這個 Layout 包含到另一個 Layout 中時(并且使用了 `<include/>` 標簽),系統會忽略 `<merge>` 標簽,直接把兩個 Button 放到 Layout 中 `<include>` 的所在位置。
                  <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>

                              哎呀哎呀视频在线观看