<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 一.作用: LayoutInflater作用是將layout的xml布局文件實例化為View類對象,LayoutInflater 的作用類似于 findViewById(),不同點是LayoutInflater是用來找layout文件夾下的xml布局文件,并且實例化!而 findViewById()是找具體某一個xml下的具體 widget控件(如:Button,TextView等)。 ### 二.獲得 LayoutInflater 實例的三種方式 1.LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater() 2.LayoutInflater inflater = LayoutInflater.from(this); 3.LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE); 其實,這三種方式本質是相同的,從源碼中可以看出: **getLayoutInflater():** Activity 的 getLayoutInflater() 方法是調用 PhoneWindow 的getLayoutInflater()方法,看一下該源代碼: ~~~ public PhoneWindow(Context context) { super(context); mLayoutInflater = LayoutInflater.from(context); } ~~~ 可以看出它其實是調用 LayoutInflater.from(context)。 LayoutInflater.from(context): ~~~ public static LayoutInflater from(Context context) { LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (LayoutInflater == null) { throw new AssertionError("LayoutInflater not found."); } return LayoutInflater; } ~~~ 可以看出它其實調用 context.getSystemService()。 結論:所以這三種方式最終本質是都是調用的Context.getSystemService()。 ### 三.實例化LayoutInflater之后,就要將layout的xml布局文件實例化為View類對象。 1. ~~~ LayoutInflater inflater = getLayoutInflater(); View view=inflater.inflate(R.layout.ID, null); ~~~ 2. ~~~ LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null); ~~~ 3. ~~~ LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.ID, null); ~~~ ### 四.inflate 方法 通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下: - public View inflate (int resource, ViewGroup root)? (常用) - inflate()方法一般接收兩個參數,第一個參數就是要加載的布局id,第二個參數是指給該布局的外部再嵌套一層父布局,如果不需要就直接傳null。這樣就成功成功創建了一個布局的實例,之后再將它添加到指定的位置就可以顯示出來了。 - public View inflate (XmlPullParser parser, ViewGroup root)? - public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)? - public View inflate (int resource, ViewGroup root, boolean attachToRoot) 示意代碼: ~~~ <pre name="code" class="java">LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test)); //EditText editText = (EditText)findViewById(R.id.content);// error EditText editText = (EditText)view.findViewById(R.id.content); ~~~ 對于上面代碼,指定了第二個參數 ViewGroup root,當然你也可以設置為 null 值。 ### 實例: 下面我們就通過一個非常簡單的小例子,來更加直觀地看一下LayoutInflater的用法。比如說當前有一個項目,其中MainActivity對應的布局文件叫做activity_main.xml,代碼如下所示: ~~~ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" > </LinearLayout> ~~~ 這個布局文件的內容非常簡單,只有一個空的LinearLayout,里面什么控件都沒有,因此界面上應該不會顯示任何東西。 那么接下來我們再定義一個布局文件,給它取名為button_layout.xml,代碼如下所示: ~~~ <Button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" > </Button> ~~~ 這個布局文件也非常簡單,只有一個Button按鈕而已。 現在我們要想辦法,如何通過LayoutInflater來將button_layout這個布局添加到主布局文件的LinearLayout中。根據剛剛介紹的用法,修改MainActivity中的代碼,如下所示: ~~~ public class MainActivity extends Activity { private LinearLayout mainLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainLayout = (LinearLayout) findViewById(R.id.main_layout); LayoutInflater layoutInflater = LayoutInflater.from(this); View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null); mainLayout.addView(buttonLayout); } } ~~~ 可以看到,這里先是獲取到了LayoutInflater的實例,然后調用它的inflate()方法來加載button_layout這個布局,最后調用LinearLayout的addView()方法將它添加到LinearLayout中。 現在可以運行一下程序,結果如下圖所示: ![](https://box.kancloud.cn/2016-02-18_56c5a956452d4.jpg) Button 在界面上顯示出來了!說明我們確實是借助LayoutInflater成功將button_layout這個布局添加到LinearLayout中了。 LayoutInflater技術廣泛應用于需要動態添加View的時候,比如在ScrollView和ListView中,經常都可以看到 LayoutInflater的身影。 參考: http://www.cnblogs.com/merryjd/archive/2013/01/08/2851092.html http://blog.csdn.net/guolin_blog/article/details/12921889
                  <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>

                              哎呀哎呀视频在线观看