1.使用include标签来加入你想包含的资源文件,操作如①所示;另外要注意的是,②③处标注的属性是Required,不写程序就会报错。当然在include里面覆盖ROOT文件里的ID的时候可以不写。但是如果要写的话,就两个必须都定义,否则不会生效。
<?xml version="1.0" encoding="utf-8"?>
<ViewGroup xmlns:android=http://schemas.android.com/apk/res/android
android:id="@[+][package:]id/resource_name"
android:layout_height=["dimension" | "fill_parent" | "wrap_content"] ---------------------------②
android:layout_width=["dimension" | "fill_parent" | "wrap_content"] [ViewGroup-specific attributes] > ---------------------------③
<View android:id="@[+][package:]id/resource_name"
android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
android:layout_width=["dimension" | "fill_parent" | "wrap_content"] [View-specific attributes] >
<requestFocus/>
</View>
<ViewGroup >
<View />
</ViewGroup>
<include layout="@layout/layout_resource"/>--------------------------①
</ViewGroup>
tag include description:
<include>
attributes:
- Layout resource. Required. Reference to a layout resource.
- Resource ID. Overrides the ID given to the root view in the included layout.
-
Dimension or keyword. Overrides the height given to the root view in the included layout. Only effective if
android:layout_width
is also declared. -
Dimension or keyword. Overrides the width given to the root view in the included layout. Only effective if
android:layout_height
is also declared.
layout
android:id
android:layout_height
android:layout_width
You can include any other layout attributes in the <include>
that are supported by the root element in the included layout and they will override those defined in the root element.
Caution: If you want to override the layout dimensions, you must override both android:layout_height
and android:layout_width
—you cannot override only the height or only the width. If you override only one, it will not take effect. (Other layout properties, such as weight, are still inherited from the source layout.)
2.使用ViewStubs视图.官方的一点文档描述如下:
A ViewStub
can be best described as a lazy include. The layout referenced by a ViewStub
is inflated and added to the user interface only when you decide so.
相当于延迟加载,想要显示你的布局文件,可以这样调用:
<ViewStub android:id="@+id/stub"
android:inflatedId="@+id/subTree"
android:layout="@layout/mySubTree"
android:layout_width="120dip"
android:layout_height="40dip" />
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
下面贴一个列子,是用第二种方法实现的类似DIV的display:none一样的隐藏功能,想学的朋友们可以自己动手创个项目把代码拷贝过去。
一 创建main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ViewStub android:id="@+id/stub"
android:inflatedId="@+id/subTree"
android:layout="@layout/stub"
android:layout_width="120dip"
android:layout_height="80dip" />
<Button
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show"
/>
</LinearLayout>
2 创建StubViews相关联的stub.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="one"
/>
<Button
android:id="@+id/sub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="two"
/>
</LinearLayout>
3 Activity类代码如下:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
private int i = 0;
private View inflated = null;
private Button sBtn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ViewStub stub = (ViewStub) findViewById(R.id.stub);
Button aBtn = (Button)findViewById(R.id.show);
aBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
System.out.println("--------1"+sBtn);
// TODO Auto-generated method stub
if(i==0){
i=1;
inflated = stub.inflate();
sBtn = (Button)TestActivity.this.findViewById(R.id.sub);
System.out.println("--------2"+sBtn);
sBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("sub commit!");
}
});
}else if(i==1){
i=2;
inflated.setVisibility(View.GONE);
}else{
i=1;
inflated.setVisibility(View.VISIBLE);
}
}
});
}
}
OVER!希望对你有用!