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

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

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

本文實例講述了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進來的組件呢。

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> 

則以上的方法將不能實現(xiàn),會報空指針。
因為用了merge后,導(dǎo)入進來就相當于是當前view下的組件了,所以直接findViewById就可以了。
 
這樣的話。。。可以解決了include 多次同一個layout的問題

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

相關(guān)文章

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

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

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

    詳解Android啟動第一幀

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

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

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

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

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

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

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

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

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

    Android中ImageView用法實例分析

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

    Android自動攔截與接聽功能APK黑白名單

    大家好,本篇文章主要講的是Android自動攔截與接聽功能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)常使用到的兩個”post“方法,關(guān)于兩者存在的區(qū)別與聯(lián)系,文章詳細分析需要的小伙伴可以參考一下
    2022-06-06
  • pp列表之分組ListView詳解

    pp列表之分組ListView詳解

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

最新評論

同德县| 潮安县| 九寨沟县| 自贡市| 荣成市| 靖边县| 博罗县| 孟村| 宝山区| 宝鸡市| 高安市| 鞍山市| 上饶县| 博白县| 盐亭县| 益阳市| 平南县| 贵南县| 德昌县| 仙桃市| 河南省| 桐柏县| 沐川县| 南京市| 正阳县| 南乐县| 延吉市| 南平市| 潍坊市| 颍上县| 缙云县| 台中市| 金川县| 赣榆县| 北京市| 沙河市| 大城县| 湄潭县| 阜阳市| 旺苍县| 温泉县|