android 使用include 调用内部组件
例子一:
sublayout.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#505050"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SubLayout"
/>
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AButton"
/>
</LinearLayout>
mail.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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"
/>
<includeandroid:id="@+id/main1"layout="@layout/sublayout"/>
<includeandroid:id="@+id/main2"layout="@layout/sublayout"/>
<Button
android:id="@+id/startanotheractivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StartAnotherActivity"
/>
</LinearLayout>
如何调用组件include进来的组件呢。
packagecom.AndroidIncludeLayout;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.Toast;
publicclassAndroidIncludeLayoutextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewsubLayout1=(View)findViewById(R.id.main1);
ViewsubLayout2=(View)findViewById(R.id.main2);
ButtonmyButton_main1=(Button)subLayout1.findViewById(R.id.mybutton);
ButtonmyButton_main2=(Button)subLayout2.findViewById(R.id.mybutton);
ButtonstartAnotherActivity=(Button)findViewById(R.id.startanotheractivity);
startAnotherActivity.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewarg0){
//TODOAuto-generatedmethodstub
Intentintent=newIntent();
intent.setClass(AndroidIncludeLayout.this,AnotherActivity.class);
startActivity(intent);
}});
myButton_main1.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewarg0){
//TODOAuto-generatedmethodstub
Toast.makeText(AndroidIncludeLayout.this,"Button1Pressed",Toast.LENGTH_LONG).show();
}});
myButton_main2.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewarg0){
//TODOAuto-generatedmethodstub
Toast.makeText(AndroidIncludeLayout.this,"Button2Pressed",Toast.LENGTH_LONG).show();
}});
}
}
但是如果include进来的xml,是
sublayout.xml
<?xmlversion="1.0"encoding="utf-8"?>
<mergexmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SubLayout"
/>
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AButton"
/>
</merge>
则以上的方法将不能实现,会报空指针。
因为用了merge后,导入进来就相当于是当前view下的组件了,所以直接
findViewById就可以了。
这样的话。。。可以解决了include多次同一个layout的问题