Android大部分的控件都会有padding和layout_margin两个属性,一般来说它们的差别是:

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之根布局动态载入子布局时边距设置无效问题的更多相关文章

  1. Android(java)学习笔记83:各种边距设置

    1. 各种边距设置: (1)android:layout_paddingLeft 内边距,对谁用,指的是谁的内部内容边距 (2)android:layout_marginLeft 外边距,对谁用,指的 ...

  2. Android(java)学习笔记141:各种边距设置

    1. android:layout_paddingLeft 内边距,对谁用,指的是谁的内部内容边距 2. android:layout_marginLeft 外边距,对谁用,指的是谁距离外层容器的边距 ...

  3. android ListView子布局中按钮响应点击事件

    只需要在子布局的根布局中添加以下属性即可: android:descendantFocusability="blocksDescendants"

  4. 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式

    注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...

  5. android 解决ScrollView中的子布局不能够填充整个ScrollView的情况。

    在开发中如果你的xml文件的跟布局是ScrollView,在ScrollView中无论你写什么样的布局,其默认情况下都是不能填充整个布局的.也就是说你的ScrollView中的子布局设置fill_pa ...

  6. 深入浅出Android动态载入jar包技术

    在实际项目中.因为某些业务频繁变更而导致频繁升级client的弊病会造成较差的用户体验,而这也恰是Web App的优势,于是便衍生了一种思路.将核心的易于变更的业务封装在jar包里然后通过网络下载下来 ...

  7. 实现Android 动态载入APK(Fragment or Activity实现)

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38565345 近期由于项目太大了.导致编译通只是(Android对一个应用中的方法个数貌似有 ...

  8. 手机端布局,rem布局动态获取根字体大小

    手机端布局,rem布局动态获取根字体大小. 以下代码: //rem布局动态获取根字体大小 function remDynamicLayout(){ var $windowWidth = $(windo ...

  9. Android动态载入Dex机制解析

    1.什么是类载入器? 类载入器(class loader)是 Java™中的一个非常重要的概念.类载入器负责载入 Java 类的字节代码到 Java 虚拟机中. Java 虚拟机使用 Java 类的方 ...

随机推荐

  1. C++标准库头文件名字和C语言头文件名字的区别

    1.C++版本的C标准库头文件,一般是cname,而C语言头文件一般是name.h 2.命名为cname的头文件中定义的名字都是从std中来的,而如果是name.h则不是这样的. 3.与是用name. ...

  2. Cracking The Coding Interview2.3

    #include <iostream> #include <string> using namespace std; class linklist { private: cla ...

  3. Cracking The Coding Interview 1.2

    //原文: // // Write code to reverse a C-Style String. (C-String means that "abcd" is represe ...

  4. MFC Release版本串口连不上的问题

    项目开发过程中发现Release版本存在连接串口时,第一次开机后,出现连接不上的问题,但在Debug版本下正常:而且只要连接上一次,Release版本就能正常连接: 解决方案: 在串口配置过程中更改为 ...

  5. PE文件 01 导入表

    0x01  导入表结构  数据目录表中的第二个成员标记了导入表的RVA和Size大小,由此可以定位到导入表: typedef struct _IMAGE_DATA_DIRECTORY { DWORD ...

  6. BeanUtils.copyProperties方法,当属性Date为null解决

    问题描述:org.apache.commons.beanutils user对象和formBean对象都有属性birthday,而且都是java.sql.Date类型的 当进行BeanUtils.co ...

  7. 为什么不同网段的ip 不能直接通信

    首先要明白一点,IOS一共七层, 发送数据的过程是从上到下,也就是从应用层一直到物理层,接受数据是从下至上. 来看你的问题,环境如下,我们来用一个ping命令的过程来解释:一个交换机,连两个电脑A和B ...

  8. a recipe kindly provided by Dimas for kikuchi

    https://sianipar17.com/2017/12/14/tutorial-for-teleseismic-body-wave-inversion-program/

  9. ChinaCock界面控件介绍-TCCImageViewerForm

    有多个图片,左右滑动可以切换,通过手势还可以放大.缩小查看,象常见的相册,就是这样子实现效果. 现在,我们有了TCCImageViewerForm组件,也可以轻松实现这样的场景应用. 现在看看TCCI ...

  10. Appium环境搭建过程中遇到的问题及解决办法

    一.[Error: Could not detect Mac OS X Version from sw_vers output: '10.12.6'] 解决办法: 1.vi /Applications ...