最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android使用include調(diào)用內(nèi)部組件的方法

 更新時(shí)間:2015年05月21日 10:31:24   作者:3H  
這篇文章主要介紹了android使用include調(diào)用內(nèi)部組件的方法,涉及Android組件調(diào)用的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了android使用include調(diào)用內(nèi)部組件的方法。分享給大家供大家參考。具體如下:

例子一:

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> 

如何調(diào)用組件include進(jìn)來的組件呢。

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進(jìn)來的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> 

則以上的方法將不能實(shí)現(xiàn),會(huì)報(bào)空指針。
因?yàn)橛昧薽erge后,導(dǎo)入進(jìn)來就相當(dāng)于是當(dāng)前view下的組件了,所以直接findViewById就可以了。
 
這樣的話。。??梢越鉀Q了include 多次同一個(gè)layout的問題

希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法

    Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法

    這篇文章主要給大家介紹了關(guān)于Android如何將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法,文中通過示例代碼介紹的非常吸納關(guān)系,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • 詳解Android啟動(dòng)第一幀

    詳解Android啟動(dòng)第一幀

    這篇文章我們就來介紹Android啟動(dòng)第一幀,至于Android第一幀什么時(shí)候開始調(diào)度,具體內(nèi)容我們就來看下面文章內(nèi)容吧,感興趣得小伙伴可以和小編一起來學(xué)習(xí)奧
    2021-10-10
  • Android開發(fā):微信授權(quán)登錄與微信分享完全解析

    Android開發(fā):微信授權(quán)登錄與微信分享完全解析

    本篇文章主要介紹了Android微信授權(quán)登錄與微信分享,具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-11-11
  • Android編程中延后處理事件的方法小結(jié)

    Android編程中延后處理事件的方法小結(jié)

    這篇文章主要介紹了Android編程中延后處理事件的方法,總結(jié)分析了Handler和TimerTask相結(jié)合以及使用postDelayed實(shí)現(xiàn)延后處理事件的相關(guān)操作技巧,需要的朋友可以參考下
    2017-03-03
  • Android?Studio使用自定義對話框效果

    Android?Studio使用自定義對話框效果

    這篇文章主要為大家詳細(xì)介紹了Android?Studio使用自定義對話框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android垃圾回收機(jī)制及程序優(yōu)化System.gc

    Android垃圾回收機(jī)制及程序優(yōu)化System.gc

    這篇文章主要介紹了Android垃圾回收機(jī)制及程序優(yōu)化System.gc的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Android中ImageView用法實(shí)例分析

    Android中ImageView用法實(shí)例分析

    這篇文章主要介紹了Android中ImageView用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了ImageView控件的功能,屬性設(shè)置與使用相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • Android自動(dòng)攔截與接聽功能APK黑白名單

    Android自動(dòng)攔截與接聽功能APK黑白名單

    大家好,本篇文章主要講的是Android自動(dòng)攔截與接聽功能APK黑白名單,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2021-12-12
  • Android中View.post和Handler.post的關(guān)系

    Android中View.post和Handler.post的關(guān)系

    這篇文章主要介紹了Android中View.post和Handler.post的關(guān)系,View.post和Handler.post是Android開發(fā)中經(jīng)常使用到的兩個(gè)”post“方法,關(guān)于兩者存在的區(qū)別與聯(lián)系,文章詳細(xì)分析需要的小伙伴可以參考一下
    2022-06-06
  • pp列表之分組ListView詳解

    pp列表之分組ListView詳解

    本篇文章是對Android中的ListView進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評論

通州市| 大荔县| 织金县| 宜宾县| 安多县| 宜兰县| 博乐市| 镇赉县| 达孜县| 中卫市| 绥中县| 高密市| 射洪县| 林芝县| 平乡县| 武隆县| 宜兰市| 威宁| 长垣县| 霍州市| 辽中县| 抚顺县| 霸州市| 巴彦淖尔市| 富裕县| 六安市| 安徽省| 汾西县| 疏附县| 玛纳斯县| 白城市| 逊克县| 双柏县| 新安县| 红桥区| 府谷县| 留坝县| 泰顺县| 纳雍县| 双鸭山市| 建始县|