Android之根布局动态加载子布局时边距设置无效问题
2014-06-13 17:30
1191 查看
Android大部分的控件都会有padding和layout_margin两个属性,一般来说它们的区别是:
padding:控件中的内容离控件边缘的距离。
margin: 控件离它的父控件边缘的距离。
今天做了一个由根布局动态加载子布局的实验,结果发现子布局中的这两个属性可以按预期的效果显示,但是给根布局设置的padding并没有对被加载的子布局产生效果。
代码如下:
根布局文件名为activity_main.xml,其xml文件定义的内容为:
上面这个根布局会添加子布局table_layout.xml中定义的布局,这个xml文件的定义内容是:
源码中实现动态加载的代码段:
但是上面这段代码执行后,table_layout布局里面的边距设置会正常显示,但是activity_main布局中table_layout的边缘却紧紧挨着activity_main的边缘,说明activity_main的padding并没有其效果。
这个问题我纠结了将近3个消失,终于设置了根局部和子布局的margin和padding也不行,分别设置top、right、bottom、left也不行,最终的解决办法却让我感到非常匪夷所思:
只需要在根布局中再加一个布局,把这个布局当做根布局来动态加载子布局就好了。
不知道为什么类型完全相同的根布局就会出错,也许'根'布局有某些特别的限制吧。
修改之后的代码是:
activity_main.xml:
源码:
如果转载请注明出处:http://blog.csdn.net/gophers?viewmode=contents
padding:控件中的内容离控件边缘的距离。
margin: 控件离它的父控件边缘的距离。
今天做了一个由根布局动态加载子布局的实验,结果发现子布局中的这两个属性可以按预期的效果显示,但是给根布局设置的padding并没有对被加载的子布局产生效果。
代码如下:
根布局文件名为activity_main.xml,其xml文件定义的内容为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="8dp" <!-- 这个布局里控件都距离它的边缘8dp --> tools:context=".MainActivity" > </LinearLayout>
上面这个根布局会添加子布局table_layout.xml中定义的布局,这个xml文件的定义内容是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tableLayout_tableName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" <!-- 这个控件离table_layout这个布局的边缘为10dp --> android:textSize="30sp" /> </LinearLayout>
源码中实现动态加载的代码段:
// 创建用于承载表的布局 LinearLayout subLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.table_layout, null); // 填充表名 tableNameTextView = ((TextView) subLayout.findViewById(R.id.tableLayout_tableName)); tableNameTextView.setText("tablename"); this.addContentView(subLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
但是上面这段代码执行后,table_layout布局里面的边距设置会正常显示,但是activity_main布局中table_layout的边缘却紧紧挨着activity_main的边缘,说明activity_main的padding并没有其效果。
这个问题我纠结了将近3个消失,终于设置了根局部和子布局的margin和padding也不行,分别设置top、right、bottom、left也不行,最终的解决办法却让我感到非常匪夷所思:
只需要在根布局中再加一个布局,把这个布局当做根布局来动态加载子布局就好了。
不知道为什么类型完全相同的根布局就会出错,也许'根'布局有某些特别的限制吧。
修改之后的代码是:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <!-- 只要这么再加一个布局来代替跟布局就OK了。。。 --> <LinearLayout android:id="@+id/mainLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="8dp" android:paddingLeft="8dp" android:paddingRight="8dp" > </LinearLayout> </LinearLayout>
源码:
LinearLayout subLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.table_layout, null); // 填充表名 tableNameTextView = ((TextView) subLayout.findViewById(R.id.tableLayout_tableName)); tableNameTextView.setText("tablename"); LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout); //通过这个新加的"根布局"来加载子布局 mainLayout.addView(subLayout);
如果转载请注明出处:http://blog.csdn.net/gophers?viewmode=contents
相关文章推荐
- Android:RecyclerView的Item布局在父布局文件设置无效问题
- Android约束布局ConstraintLayout动态设置Id失效问题解决办法
- Android中xml布局文件中使用include引入布局进行复用(解决使用include布局重叠,颜色设置无效问题)
- 通过窗口大小适时调整以及子窗口的动态布局来解决窗口在不同DPI设置下的显示问题
- Android程序开发中关于设置全屏无效问题
- android 通过setalpha设置透明度 无效及3.0以下不兼容的问题
- 【Android疑难杂症】GridView动态设置Item的宽高导致第一个Item不响应或显示不正常的问题
- [Android疑难杂症]动态改变Background后Padding无效的问题
- android 在代码中动态设置字体颜色需要注意的问题
- Android软键盘弹出,RelativeLayout 布局会把原来的界面挤上去的问题,设置了隐藏键盘也没效果
- cocos2d-x在android真机上设置帧率无效的问题
- Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标
- Javascript动态为Select控件添加项并设置控件值无效的问题
- Table布局width设置为百分比无效问题
- android 通过setalpha设置透明度 无效及3.0以下不兼容的问题
- 解决Android当中GridView的Item子控件中有ImageView图片大小不一致问题,动态设置ImageView控件大小
- Android控件动态设置style的问题
- android代码中动态设置布局中控件的属性
- GridView设置android:numColumns="auto_fit"属性无效的问题解决
- android调用系统裁剪自由比例,设置输出尺寸无效的问题