欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android使用include调用内部组件的方法

程序员文章站 2022-05-16 12:17:26
本文实例讲述了android使用include调用内部组件的方法。分享给大家供大家参考。具体如下: 例子一: sublayout.xml

本文实例讲述了android使用include调用内部组件的方法。分享给大家供大家参考。具体如下:

例子一:

sublayout.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="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=" a button " 
  /> 
</linearlayout> 
mail.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" 
  /> 
<include android:id="@+id/main1" layout="@layout/sublayout" /> 
<include android:id="@+id/main2" layout="@layout/sublayout" /> 
<button 
  android:id="@+id/startanotheractivity" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text=" start another activity " 
  /> 
</linearlayout> 

如何调用组件include进来的组件呢。

package com.androidincludelayout; 
import android.app.activity; 
import android.content.intent; 
import android.os.bundle; 
import android.view.view; 
import android.widget.button; 
import android.widget.toast; 
public class androidincludelayout extends activity { 
  /** called when the activity is first created. */ 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
    view sublayout1 = (view)findviewbyid(r.id.main1); 
    view sublayout2 = (view)findviewbyid(r.id.main2); 
    button mybutton_main1 = (button)sublayout1.findviewbyid(r.id.mybutton); 
    button mybutton_main2 = (button)sublayout2.findviewbyid(r.id.mybutton); 
    button startanotheractivity = (button)findviewbyid(r.id.startanotheractivity); 
    startanotheractivity.setonclicklistener(new button.onclicklistener(){ 
  @override 
  public void onclick(view arg0) { 
  // todo auto-generated method stub 
  intent intent = new intent(); 
       intent.setclass(androidincludelayout.this, anotheractivity.class); 
       startactivity(intent); 
  }}); 
    mybutton_main1.setonclicklistener(new button.onclicklistener(){ 
  @override 
  public void onclick(view arg0) { 
  // todo auto-generated method stub 
  toast.maketext(androidincludelayout.this, "button 1 pressed", toast.length_long).show(); 
  }}); 
    mybutton_main2.setonclicklistener(new button.onclicklistener(){ 
  @override 
  public void onclick(view arg0) { 
  // todo auto-generated method stub 
  toast.maketext(androidincludelayout.this, "button 2 pressed", toast.length_long).show(); 
  }}); 
  } 
}

但是如果include进来的xml,是

sublayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns: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=" a button " 
  /> 
</merge> 

则以上的方法将不能实现,会报空指针。
因为用了merge后,导入进来就相当于是当前view下的组件了,所以直接findviewbyid就可以了。
 
这样的话。。。可以解决了include 多次同一个layout的问题

希望本文所述对大家的android程序设计有所帮助。