`

android加载include

    博客分类:
  • UI
阅读更多

例子一: 
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就可以了。

 

01.<?xml version="1.0" encoding="utf-8"?>  

02.<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" style="@style/StyleLayoutMain" mce_style="@style/StyleLayoutMain"  

03.    xmlns:android="http://schemas.android.com/apk/res/android">  

04.      

05.    <!-- include标签内不能设置RelativeLayout属性,如android:layout_alignParentBottom,因为不起作用 -->  

06.    <!-- include标签内设置id属性后(android:id),其引用的布局layout内的id属性就不起作用了,怀疑是其引用的layout外层包裹了一层include标签   

07.        或者是覆盖了其内的属性id-->  

08.    <!-- 如果没有include标签,所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差。而且界面加载的时候是按照顺序加载的,前面的布局不能  

09.        调用其后面的布局id。而采用include后,一个include中可以引用其后的include中的布局id属性 -->  

10.    <include android:id="@id/titleLayout" layout="@layout/app_title" />  

11.  

12.    <include layout="@layout/app_tradelogin"/>  

13.          

14.    <include layout="@layout/app_bottom"/>  

15.      

16.</RelativeLayout>  

 

分享到:
评论

相关推荐

    Android下使用ViewStub控件加载

    Android下使用ViewStub控件动态懒加载。可以参见博客:http://www.cnblogs.com/plokmju/p/android_ViewStub.html

    Android实现加载状态视图切换效果

    关于Android加载状态视图切换,具体内容如下 1.关于Android界面切换状态的介绍 怎样切换界面状态?有些界面想定制自定义状态?状态如何添加点击事件?下面就为解决这些问题! 内容界面 加载数据中 加载数据...

    Android代码-Android TextureView 和SurfaceView 简单动画库

    A simple but powerful Tween / SpriteSheet / ParabolicMotion / animation library for Android TextureView and SurfaceView. Features The controls Fps possible to animation. Possible for animation of ...

    Android在layout xml中使用ViewStub完成动态加载问题

     &lt;include layout=@layout/otherLayout/&gt;  2、动态加载:需要被加载的模块初始时并没有被加载进内存,在你需要加载这个模块才会被动态的加载进去。  &lt;ViewStub android:layout=@layout/otherLayout/&gt;  还要把...

    新版Android开发教程.rar

    In particular, note that some Linux distributions may include JDK 1.4 or Gnu Compiler for Java, both of which are not supported for Android development----------------------------------- Android 编程...

    include标签的使用

    由于Android系统对硬件的要求较高,并且上层应用都是用Java(效率要比C++低)编写的,对程序的优化就成了...可以从以下几个地方下手:布局优化、数据库优化、使用异步加载数据、使用缓存技术、算法代码优化、使用线程池

    Android代码-LoadDataLayout

    项目中经常会遇到几种页面:加载中、无网络、无数据、出错四种情况,传统的方式是通过include相关的布局,逐个分情况设置显示或隐藏,这样繁琐的过程一直是个痛点,于是参考了 Weavey 的封装套路,自己重新写了一套...

    Android代码-Gank

    RecycleView 的使用,下拉刷新,上拉到底自动加载等; vitamio播放视频和自定义控制器 Activity滑动返回的实现 Android 抓包及逆向分析 使用到的库 dependencies { compile project(':vitamio') compile fileTree...

    react-native-android-fragment:已弃用:一个实用程序库,用于通过Android Fragments促进React Native开发

    已淘汰 该库已被弃用,Hudl不再使用。 如果您一直在使用此库,请考虑在主要的本机repo中采用 ...将以下行添加到android/settings.gradle : include ':react-native-android-fragment' project(':react-native-android

    fake-linker:修改Android链接器以提供加载模块和挂钩功能

    修改Android链接器以提供加载模块和plt钩子功能。请检查详细原理, 支援的Android Android版本: Android 5.0 Android 11 +。 支持指令: x86 , x86_64 , arm , arm64 建造 源代码构建 将其作为Android Library...

    0xA03 Android 10 源码分析:APK 加载流程之资源加载

    系统对 merge、include 是如何处理的 merge 标签为什么可以起到优化布局的效果? XML 中的 View 是如何被实例化的? 为什么复杂布局会产生卡顿?在 Android 10 上做了那些优化? BlinkLayout 是什么?

    Android实现仿网易新闻主界面设计

    下面先来一张效果图    根据图片分析,要实现的有侧边栏DrawerLayout,ActionBar的颜色和菜单以及ActionBarDrawerToggle的动画效果....FrameLayout为主视图,include加载的则为左侧边栏,因此是start属性 &lt;androi

    Android使用jni调用c++/c方法详解

    JniTest是在Android.mk里约束好的,关于Android.mk的编写具体在后面详解。 3、使用javah -jni生成.h文件 编写好jni加载类之后,就要开始生成.h文件了,此文件相当于一个声明文件,起到jni连接c++源

    pcf8563_i2c1_r8_ruoge_ov2640通过给RTC驱动增加设备节点读取秒钟成功+直接读取I2C1获取秒钟值20160626_2201.7z

    root@android:/dev # cd /sys/class/i2c-adapter/ root@android:/sys/class/i2c-adapter # ll lrwxrwxrwx root root 1970-01-02 08:31 i2c-0 -&gt; ../../devices/platform/sun5i-i2c.0/i2c-0 lrwxrwxrwx root root ...

    android studio无法添加 bmob sdk依赖问题及解决方法

    1,关闭 app的 grader.build下的implementation 对应sdk依赖。注释掉 2,去bmob官网下载sdk 压缩包,解压,找到lib文件夹。...4,在app的gradler.build里include libs库。并且创建so链接。 implementation

    Android Studio编写微信页面提交功能

    页面的显示上,我使用的是在主页面上使用FrameLayout组件作为中间的主要显示区域,然后顶部和底部则使用include进行引入。对于页面的布局没有什么多说的,唯一要注意的就是页面的布局上,高度调整 &lt;!--一下为...

    LCRapidDevelop-master

    compile fileTree(include: ['*.jar'], dir: 'libs') .... compile project(':lcrapiddeveloplibrary') } ##轻松实现异常统一管理 MyApplication里面初始化就可以了 ``` public class MyApplication extends ...

    react-native-splash-screen:一个用于react-native的初始屏幕,在加载应用程序时隐藏,可在iOS和Android上运行

    在您的android/settings.gradle文件中,添加以下内容: include ' :react-native-splash-screen ' project( ' :react-native-splash-screen ' ) . projectDir = new File (rootProject . projectDir, ' ../node_...

    高焕堂<应用框架原理与程序设计>源代码(Eclipse)

    高焕堂&lt;应用框架原理与程序设计&gt;源代码(Eclipse) 36计本人全部测试通过。比较难的地方而书中没有相关解释的有加...工程中记得加入NativeHalfAdder.java 文件,该文件加载了库文件并声明外部方法 运行本程序 大功告成

    NDKSignalTest

    NDKSignalTest 显示在Android 4.2及更高版本的设备上崩溃(大多数是经过Nexus设备测试的) ... 编辑jni/Android.mk文件以注释include $(BUILD_SHARED_LIBRARY)行,并取消注释include $(BUILD_EXECUTABLE)行

Global site tag (gtag.js) - Google Analytics