android使用include調(diào)用內(nèi)部組件的方法
本文實(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),會報空指針。
因?yàn)橛昧薽erge后,導(dǎo)入進(jìn)來就相當(dāng)于是當(dāng)前view下的組件了,所以直接findViewById就可以了。
這樣的話。。??梢越鉀Q了include 多次同一個layout的問題
希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
- Android抽象布局——include、merge 、ViewStub詳解
- Android編程之include文件的使用方法
- Android中使用include標(biāo)簽和merge標(biāo)簽重復(fù)使用布局
- 解析android中include標(biāo)簽的使用
- Android開發(fā)之merge結(jié)合include優(yōu)化布局
- Android開發(fā)技巧之ViewStub控件惰性裝載
- 深入分析Android ViewStub的應(yīng)用詳解
- Android組件ViewStub基本使用方法詳解
- Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用
相關(guān)文章
activity 獲取rootView 設(shè)置backGroundColor的方法
下面小編就為大家?guī)硪黄猘ctivity 獲取rootView 設(shè)置backGroundColor的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android抽象布局——include、merge 、ViewStub詳解
這篇文章主要介紹了 Android抽象布局——include、merge 、ViewStub詳解,詳細(xì)的介紹了三種布局各有的優(yōu)勢,有興趣的同學(xué)可以參考一下。2016-12-12Android中dip、dp、sp、pt和px的區(qū)別詳解
本篇文章是對Android中dip、dp、sp、pt和px的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android實(shí)現(xiàn)類似IOS右滑返回的效果(原因分析及解決辦法)
這篇文章主要介紹了Android實(shí)現(xiàn)類似IOS右滑返回的效果,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-03-03Android自定義View之邊框文字、閃爍發(fā)光文字
這篇文章主要為大家詳細(xì)介紹了Android自定義View之邊框文字、閃爍發(fā)光文字,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android實(shí)現(xiàn)頂部導(dǎo)航欄可點(diǎn)擊可滑動效果(仿微信仿豆瓣網(wǎng))
這篇文章主要介紹了 Android實(shí)現(xiàn)頂部導(dǎo)航欄可點(diǎn)擊可滑動效果(仿微信仿豆瓣網(wǎng)),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03