http://androidbiancheng.blogspot.com/2011/01/includeui.html
折腾了一晚上,搞明白了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就可以了。
分享到:
相关推荐
主要介绍了android使用include调用内部组件的方法,涉及Android组件调用的相关技巧,需要的朋友可以参考下
Android中include和merge标签的基本使用方法
android include merge标签
android中include和merge标记的区别和使用
我们是给这些个Activity布局文件都统一加上一样的布局代码,但是维护起来很麻烦,修改不方便,有没有一种类似于编程语言的include语法呢?答案是有的,但是sdk的demo并没有说出使用方法,但这并不说明不好使用,其实...
关于xml布局中标签<include />的使用的一个Demo
该资源主要是参考博客[Android] 使用Include布局+Fragment滑动切换屏幕的文章 主要讲述使用Include布局,同时像微信一样实现动态切换屏幕的效果.免费资源希望对大家有所帮助.
Android中Include的使用
出错,include后调用不了,函数 stdafx.h里最下一行写#include "game_fg.h" 然后game_fg.h里面的函数CString f_gee()这个在 chuchuo_includeDlg.cpp,dd.cpp,这两个文件里面 void Cchuchuo_includeDlg::...
Android UI优化之include标签使用,主要介绍include使用方法及注意事项。
JniTest是在Android.mk里约束好的,关于Android.mk的编写具体在后面详解。 3、使用javah -jni生成.h文件 编写好jni加载类之后,就要开始生成.h文件了,此文件相当于一个声明文件,起到jni连接c++源
AndroidStudio环境下的jni调用(NDK),这个教程可以让完全不会AndroidStudio的人学会jni调用。完全步骤话,非常简单。
android ffmpeg include 开发ffmpeg需要的头文件
主要介绍了Android编程之include文件的使用方法,实例分析了Android使用include引入文件的技巧,有助于增加代码的重用性,提高程序设计效率,需要的朋友可以参考下
为了高效复用完整布局,你可以使用<include>和标签嵌入另一个布局到当前布局。所以当你通过写一个自定义视图创建独立UI组件,你可以放到一个布局文件里,这样更容易复用。 复用布局因为其允许你创建可复用的复杂布局...
为android平台编译的ffmpeg so库 和 对用的 include文件,方便直接在android studio中进行ndk开发,来源于博客https://blog.csdn.net/zhangpengzp/article/details/82289224 使用so的方法: ...
本篇文章是对android中include标签的使用进行了详细的分析介绍,需要的朋友参考下