前言
兩個都是布局加載器,而View.inflate是對 LayoutInflater.from(context).inflate的封裝,功能相同,案例使用了dataBinding。
View.inflate(context, layoutResId, root)
LayoutInflater.from(context).inflate(layoutResId, root, false)
區別
因為View.inflate(context,layoutResId,root)?比 LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) 少了一個attachToRoot參數(是否將layoutResId添加到某個View中,作為其子View)。
在使用View.inflate(context,layoutResId,root)?時,如果root(父View)是null,會導致layoutResId布局中聲明的寬高 + 外邊距參數,失效。
核心條件就是root(父View)是不是null。
案例
1、使用View.inflate(context,layoutResId,root)?root不為null
private AppActivityMainBinding bind;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind = AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main,null)); setContentView(bind.getRoot());// 子布局:R.layout.app_layout_text // 父布局:bind.boxView.inflate(this,R.layout.app_layout_text,bind.box);View.inflate(this,R.layout.app_layout_text,bind.box);View.inflate(this,R.layout.app_layout_text,bind.box);}
2、使用LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) ?root不為null,且attachToRoot是true
private AppActivityMainBinding bind;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind = AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main,null)); setContentView(bind.getRoot());// 子布局:R.layout.app_layout_text // 父布局:bind.boxLayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);}
兩種方式效果相同,寬高 + 外邊距 都有效
3、使用View.inflate(context,layoutResId,root)?root為?null
private AppActivityMainBinding bind;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind = AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main,null)); setContentView(bind.getRoot());// 子布局:R.layout.app_layout_text // 父布局:bind.boxView view = View.inflate(this, R.layout.app_layout_text, null);View view2 = View.inflate(this, R.layout.app_layout_text, null);View view3 = View.inflate(this, R.layout.app_layout_text, null);bind.box.addView(view);bind.box.addView(view2);bind.box.addView(view3);}
4、使用LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) ?root為?null,且attachToRoot是false
private AppActivityMainBinding bind;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind = AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main,null)); setContentView(bind.getRoot());// 子布局:R.layout.app_layout_text // 父布局:bind.boxView view = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);View view2 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);View view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);bind.box.addView(view);bind.box.addView(view2);bind.box.addView(view3);}
兩種方式效果相同,寬高 + 外邊距 都失效了,寬/高?變成wrap_content,一點要記住這點!!!是變成wrap_content。
至于為什么layoutResId布局寬度和父View一樣,當子View失去自身LayoutParams(布局參數)后,父View會自動調整子View的寬高屬性,下面會講,先忽略。
5、如果不想將layoutResId布局添加到父View中,同時又不想丟失layoutResId布局中聲明的參數,LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot)這樣寫可以做到,root不為null,但是attachToRoot為false
private AppActivityMainBinding bind;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind = AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main,null)); setContentView(bind.getRoot());// 子布局:R.layout.app_layout_text // 父布局:bind.boxView view = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);View view2 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);View view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);bind.box.addView(view);bind.box.addView(view2);bind.box.addView(view3);}
效果
6、而View.inflate(context,layoutResId,root)?目前為止無法做到,因為它少了一個attachToRoot參數(是否將layoutResId添加到某個View中,作為其子View),以后說不準會有這個參數的重載方法。
7、案例文件:shape_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><stroke android:color="@color/color_303133" android:width="1dp"/>
</shape>
8、案例文件:app_layout_text.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="300dp"android:layout_height="200dp"android:layout_marginBottom="20dp"android:background="@drawable/shape_border"android:paddingLeft="20dp"android:paddingTop="50dp"android:text="測試" />
9、案例文件:app_activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><data></data><LinearLayoutandroid:id="@+id/box"android:orientation="vertical"android:background="@color/color_14F9230A"android:layout_width="match_parent"android:layout_height="match_parent"></LinearLayout></layout>
10、案例文件:AppMainActivity.Java
public class AppMainActivity extends AppCompatActivity {private AppActivityMainBinding bind;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind = AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main,null));setContentView(bind.getRoot());// View.inflate(this,R.layout.app_layout_text,bind.box);// View.inflate(this,R.layout.app_layout_text,bind.box);// View.inflate(this,R.layout.app_layout_text,bind.box);// View view = View.inflate(this, R.layout.app_layout_text, null);// View view2 = View.inflate(this, R.layout.app_layout_text, null);// View view3 = View.inflate(this, R.layout.app_layout_text, null);// bind.box.addView(view);// bind.box.addView(view2);// bind.box.addView(view3);// LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);// LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);// LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);// View view = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);// View view2 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);// View view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);// bind.box.addView(view);// bind.box.addView(view2);// bind.box.addView(view3);// View view = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);// View view2 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);// View view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, null, false);// bind.box.addView(view);// bind.box.addView(view2);// bind.box.addView(view3);}
源碼解析
View.inflate源碼,還是調用的LayoutInflater.from(context).inflate。
public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {LayoutInflater factory = LayoutInflater.from(context);return factory.inflate(resource, root);}
LayoutInflater.java源碼
第一判斷條件是root(父布局)是否為null,第二判斷條件就是attachToRoot,View.inflate沒有這個參數。
... ... View result = root;... ... if (root != null) {// Create layout params that match root, if suppliedparams = root.generateLayoutParams(attrs);if (!attachToRoot) {// Set the layout params for temp if we are not// attaching. (If we are, we use addView, below)temp.setLayoutParams(params);}}... ... return result;
父View自動調整子View的寬高
當子View?失去或沒有 自身LayoutParams(布局參數)后,父View會自動調整子View的寬高。
布局類型不同,子View寬高值也不同,說幾個常用布局:
FrameLayout:寬?/?高 都會變成match_parent
RelativeLayout 和?ConstraintLayout 一樣,寬?/?高 都會變成wrap_content
LinearLayout 設置vertical(垂直方向):寬變成match_parent,高變成wrap_content
LinearLayout 設置horizontal(水平方向):寬 /?高 都會變成wrap_content
總結
只有在實例化layoutResId布局時,而又不想 作為子View、不想丟失聲明的參數,它倆才會有使用區別。
順便說一下返回值,layoutResId布局作為子View時,返回的是父布局View,反之返回的是layoutResId布局View,這一點它們是一樣的。
View view = View.inflate(this, R.layout.app_layout_text, bind.box);Log.d("TAG","父布局LinearLayout:"+(view instanceof LinearLayout)); // trueLog.d("TAG","當前布局TextView:"+(view instanceof TextView)); // falseView view2 = View.inflate(this, R.layout.app_layout_text, null);Log.d("TAG","父布局LinearLayout:"+(view2 instanceof LinearLayout)); // falseLog.d("TAG","當前布局TextView:"+(view2 instanceof TextView)); // trueView view3 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, true);Log.d("TAG", "父布局LinearLayout:" + (view3 instanceof LinearLayout)); // trueLog.d("TAG", "當前布局TextView:" + (view3 instanceof TextView)); // falseView view4 = LayoutInflater.from(this).inflate(R.layout.app_layout_text, bind.box, false);Log.d("TAG", "父布局LinearLayout:" + (view4 instanceof LinearLayout)); // falseLog.d("TAG", "當前布局TextView:" + (view4 instanceof TextView)); // true