国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android使用include調用內部組件的方法

2020-04-11 11:32:58
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 土默特左旗| 长泰县| 社会| 芜湖县| 台前县| 海安县| 淮北市| 南雄市| 台东县| 洛南县| 新昌县| 清流县| 庆安县| 邹平县| 兴文县| 周至县| 岚皋县| 车致| 琼结县| 郯城县| 亳州市| 广元市| 尼勒克县| 穆棱市| 左贡县| 怀柔区| 抚宁县| 上栗县| 东丰县| 海门市| 汉源县| 建瓯市| 岢岚县| 普兰县| 临海市| 湖口县| 上思县| 青铜峡市| 锡林浩特市| 西藏| 冀州市|