在应用中自定义一个view,需要获取这个view的布局,需要用到
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
这个方法。
一般的资料中的第二个参数会是一个null。通常情况下没有问题,但是如果我想给这个view设置一个对应的类,然后通过这个类来操作的话就会出问题。
先看下面的例子
<?xml version="1.0" encoding="utf-8"?> <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/white"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewTitle" android:textColor="@color/black" android:gravity="center" android:textSize="26dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewAuthor" android:layout_gravity="left" android:textColor="@android:color/darker_gray" android:textSize="16dp"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_gravity="center_horizontal" android:scaleType="center"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewContent" android:layout_gravity="center_horizontal" android:textColor="@color/black" android:textSize="20dp"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="2dp" android:layout_gravity="center" android:background="@color/black"> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewOtherInfo" android:layout_gravity="left" android:clickable="true" android:textColor="@android:color/darker_gray" android:textSize="16dp"/> </LinearLayout>
对应的类是
public class ContentItemView extends LinearLayout { private TextView title; private TextView author; private TextView content; private TextView otherInfo; private ImageView contentImage; private ContentInfo info; public ContentItemView(Context context) { super(context); init(context); } private void init(Context context) { LinearLayout convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null); title = (TextView) convertView.findViewById(R.id.textViewTitle); author = (TextView) convertView.findViewById(R.id.textViewAuthor); content = (TextView) convertView.findViewById(R.id.textViewContent); otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo); contentImage = (ImageView) convertView.findViewById(R.id.imageView); } }
这个自定义view我想将它添加到一个listview中。
public void add(final ContentInfo info) { ContentItemView contentItemView = new ContentItemView(context); contentItemView.setContentInfo(info); contentItemView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT)); data.add(contentItemView); } @Override public View getView(int position, View convertView, ViewGroup parent) { return data.get(position); }
程序运行起来以后,没有任何问题,但是界面没有显示出来,仅仅是在listview中多了一系列黑色的条条
如果将
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
修改为
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, this);
显示就会正常
上面的东西很多资料里面都有,但是原因是什么?我在网络上找了很久都没有找到,于是就自己研究了下代码
public View inflate(int resource, ViewGroup root) { return inflate(resource, root, root != null); } public View inflate(int resource, ViewGroup root, boolean attachToRoot) { if (DEBUG) System.out.println("INFLATING from resource: " + resource); XmlResourceParser parser = getContext().getResources().getLayout(resource); try { return inflate(parser, root, attachToRoot); } finally { parser.close(); } } public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) { ........ if (TAG_MERGE.equals(name)) { if (root == null || !attachToRoot) { throw new InflateException("<merge /> can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } rInflate(parser, root, attrs, false); } else { // Temp is the root view that was found in the xml View temp; if (TAG_1995.equals(name)) { temp = new BlinkLayout(mContext, attrs); } else { temp = createViewFromTag(root, name, attrs); } ViewGroup.LayoutParams params = null; if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = 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); } } .............. if (root != null && attachToRoot) { root.addView(temp, params); } // Decide whether to return the root that was passed in or the // top view found in xml. if (root == null || !attachToRoot) { result = temp; } } ..... }
可以看到在inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)函数中,只有root不等于空的情况下才能够真正的把view添加到listview中。
看看参数root的含义:@param root Optional view to be the parent of the generated hierarchy
就是说这个表示的事view的容器是什么。如果不告诉SDK你要把这个view放到哪里,当然就不能生成view了。