Android-xml根布局-参数失效原因
来源:互联网 发布:美国政治正确 知乎 编辑:程序博客网 时间:2024/06/05 17:35
1.item.xml根布局参数没有添加到父布局
1).在item.xml中<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="32dp" android:background="#00f" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Text" android:textColor="#fff" /></LinearLayout>2).在Adapter中@Overridepublic View getView(int position, View convertView, ViewGroup parent) { convertView = View.inflate(context, R.layout.item, null);// convertView = LayoutInflater.from(context).inflate(R.layout.item, null, false);// convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false); return convertView;}3).在Dialog中Dialog d= new Dialog(this); d.setContentView(View.inflate(context, R.layout.item, null));// d.setContentView(R.layout.item);d.show();4).在Activity中 @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(View.inflate(context, R.layout.item, null)); // setContentView(R.layout.item);}1.现象:在Adapter, Dialog, Activity中的item.xml根布局参数全部失效! 2.原因:通过xxx.inflate(R.layout.item, null, false)返回的view没保存item.xml根布局参数,item.xml根布局参数是与父布局相关,如果没有父布局就不会存item.xml根布局参数!3.解决办法:1).在Adapter中推荐使用LayoutInflater.from(context).inflate(R.layout.item, parent, false);把item.xml根布局参数存到parent,false表明不把xml加到parent,只保存xml布局参数!2).在Adapter, Dialog中无法获取item.xml的父布局,推荐使用setContentView(R.layout.activity_main),在setContentView内部会用xxx.inflate(R.layout.item, parent,..)保存item.xml根布局参数!
2.在Adapter中的item.xml根布局layout_margin失效
原因: 在Adapter中的item.xml的父布局是ListView,而ListView继承于AbsListView的布局参数没有layout_margin LayoutParams继承体系: ViewGroup.LayoutParams AbsListView.LayoutParams 没继承ViewGroup.MarginLayoutParams,所以layout_margin不可用 ViewPager.LayoutParams AbsoluteLayout.LayoutParams WindowManager.LayoutParams Gallery.LayoutParams ViewGroup.MarginLayoutParams 定义layout_margin等参数 ActionBar.LayoutParams GridLayout.LayoutParams FrameLayout.LayoutParams LinearLayout.LayoutParams RelativeLayout.LayoutParams 总结: 特别注意以下几个布局容器 AbsListView.LayoutParams ViewPager.LayoutParams AbsoluteLayout.LayoutParams WindowManager.LayoutParams Gallery.LayoutParams 只继承了ViewGroup.LayoutParams,以layout_xxx开头参数只有layout_width,layout_height被定义了, 其它layout_xxx参数分别定义在FrameLayout,LinearLayout,RelativeLayout。
简书: http://www.jianshu.com/p/b2b845f3a836
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/70462232
GitHub博客:http://lioil.win/2017/04/22/Android-LayoutParamsInvalid.html
Coding博客:http://c.lioil.win/2017/04/22/Android-LayoutParamsInvalid.html
0 0
- Android-xml根布局-参数失效原因
- 在Android的TableLayout布局中,column和span属性“失效”原因分析与解决
- Android中removeCallbacks失效原因
- android selector失效的原因
- shareSDK.xml 失效,分享失败原因
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- android:布局参数,控件属性及各种xml的作用
- android:布局参数,控件属性及各种xml的作用
- android 布局常用参数 xml VS代码篇
- android:布局参数,控件属性及各种xml的作用
- android:布局参数,控件属性及各种xml的作用
- android:布局参数,控件属性及各种xml的作用
- android:布局参数,控件属性及各种xml的作用
- android:布局参数,控件属性及各种xml的作用
- android:布局参数,控件属性及各种xml的作用
- DIV+CSS网页布局中CSS失效的原因
- DrawerLayout抽屉布局item点击事件失效原因之一
- Django学习4-模板表达式
- Linux Ubuntu/Centos7 定时备份mysql数据库
- 用jq写轮播图(目前只是半成品)
- 如何用好 github 中的 watch、star、fork,转载mark
- bzoj 1079: [SCOI2008]着色方案 (记忆化搜索)
- Android-xml根布局-参数失效原因
- spring mvc 返回json实例
- 超级素数幂
- 扩展ThreadPoolExector
- 如何解决Android eclipse 使用RecyclerView和CardView控件时报错问题
- Android-6.0之PMS安装APK下篇
- gcc和gdb的使用
- Python 创建线程
- 浅谈React实现搜索匹配