分别来看三种情况的inflate()方法
- Activity中
- BaseAdapter getView()方法中
- Fragment onCreateView()方法中
Activity中
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
View view = getLayoutInflater().inflate(R.layout.content, null);
container.addView(view);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorAccent"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/app_name" />
</LinearLayout>
给inflate()传不同的参数
- View view = getLayoutInflater().inflate(R.layout.content, null);
- View view = getLayoutInflater().inflate(R.layout.content, container, false);
- View view = getLayoutInflater().inflate(R.layout.content, container, true);
抛异常:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.
现在去掉container.addView(view);
- View view = getLayoutInflater().inflate(R.layout.content, container, false);
- View view = getLayoutInflater().inflate(R.layout.content, container, true);
可以看到:
inflate(R.layout.content, null);会导致content.xml中根元素设置的android:layout_height=”300dp” 不起作用
inflate(R.layout.content, container, false);android:layout_height=”300dp” 会生效
此时如果不进行container.addView(view); 将会什么都不显示, 也就是没有添加到父布局container中.
而inflate(R.layout.content, container, true);的时候, 就不能进行container.addView(view), 因为attachToRoot为true的时候, 已经添加到添加到父布局container中了.
BaseAdapter getView()方法中
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView);
mListView.setAdapter(new MAdapter());
}
class MAdapter extends BaseAdapter {
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getLayoutInflater().inflate(R.layout.content, null);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/colorAccent"
android:dividerHeight="2dp" />
</LinearLayout>
content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/app_name" />
</LinearLayout>
给inflate()传不同的参数
- return getLayoutInflater().inflate(R.layout.content, null);
- return getLayoutInflater().inflate(R.layout.content, parent, false);
- return getLayoutInflater().inflate(R.layout.content, parent, true);
抛异常:
Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
同上面一样
inflate(R.layout.content, null);会导致content.xml中根元素设置的android:layout_height=”300dp” 不起作用
inflate(R.layout.content, container, false);android:layout_height=”300dp” 会生效, Item高度为300dp
Fragment onCreateView()方法中
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.container, new MFragment())
.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
MFragment
public class MFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.content, null);
}
}
- return inflater.inflate(R.layout.content, null);
- return inflater.inflate(R.layout.content, container, false);
- return inflater.inflate(R.layout.content, container, true);
抛异常:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.
- inflater.inflate(R.layout.content, container, true);
- return null;
显示和第二种一样, 但是没什么意义.