include标签使用小结
1.
根容器id与include id必须相同?
A:可以不相同,程序照常运行,但是会有如下问题
// 公用布局
<include
android:id="@+id/layout_1"
layout="@layout/include_child"
android:layout_width="100dp"
android:layout_height="100dp" />
// -----------------------------------------------------
// include_child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/layout_2"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_id"
android:layout_width="100dp"
android:layout_height="100dp" />
</RelativeLayout>
// -----------------------------------------------------
View layout = findViewById(R.id.layout_1);
Log.i(TAG, "layout " + layout); // 此时正常获取Layout
View layout = findViewById(R.id.layout_2);
Log.i(TAG, "layout " + layout); // 此时返回null
2.
使用include时,除了使用layout属性加载布局文件时,一般不需要设置其他属性了
// android:layout_margin="10dp"生效必须加上宽,高属性
<include
android:id="@+id/layout_1"
layout="@layout/include_child"
android:layout_width="100dp"
android:layout_margin="10dp"
android:layout_height="100dp" />
A:但有时为了布局的需要,仍要使用诸如layout_margin等除id之外的其他属性,这时就要注意,为了使这些其他属性起作用,必须同时设置include标签的宽高属性
3.
android:paddingBottom
这种属性使用在include标签中,失效