标签:android
一、首先看带三个参数的inflate方法:查看源码:
public View inflate(int resource, ViewGroup root) { return inflate(resource, root, root != null); }也就是说
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dip" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" /> </LinearLayout>
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflate(R.layout.item_lv_test, null); } return convertView; }如果用上面的代码会发现设置100dp是无效的。而如果换成下面的代码就可以了。
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflate(R.layout.item_lv_test, parent, false); } return convertView; }
参考:http://blog.csdn.net/guolin_blog/article/details/12921889
https://github.com/CharonChui/AndroidNote/blob/master/Android%E5%AD%A6%E4%B9%A0%E5%8A%A0%E5%BC%BA/LayoutInflater.inflate%E8%AF%A6%E8%A7%A3.md
【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
标签:android
原文地址:http://blog.csdn.net/u011494050/article/details/42555139