在java代码中加载布局 java代码加载顺序

.请简述如何在Java代码与XML文件中调用布局资源文件

步骤:

公司主营业务:成都网站设计、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出防城港免费做网站回馈大家。

1、在存放使用资源的res文件夹下的layout文件夹内新建一个XML布局文件,如命名为:page1.xml。

2、在存放资、代码的文件夹下下找到MainActivity.java,双击打开,在onCreate的方法内添加关联代码。

android 如何利用java代码 在一个xml布局中插入另一个xml布局

Android在xml文件中可使用include包含其他定义好的布局, 可以将多处用到的布局单独出来,然后用include包含进来,这种包含方法相当于把原来布局的一部分代码独立出来,供大家共同使用,也就相当于面向对向中的类的概念差不多。下面我们逐步讲解include的作用。

先看下我们要实现的整体界面:

一、未使用Include时

通常情况下,我们直接就能写出布局代码,下面是所使用的XML代码:

[html] view plaincopy

?xml version="1.0" encoding="utf-8"?

LinearLayout xmlns:android=""    

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" 

!-- 第一部分 --

TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#ff0000"

android:text="第一个BTN" /

Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" One Button " /

!-- 第二部分 --

TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#00ff00"

android:text="第二个BTN" /

Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" Second Button " /

!-- 最后的按钮 --

Button

android:id="@+id/another"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" Another Button " /

/LinearLayout

这段代码理解起来一点难度没有,就是几个TextView和几个Button,下面我们用include把这段代码给分割成几个文件,并完成相同的效果;

二、使用Include时

1、先将上面代码标记有“第一部分”的,代码段分离成一个文件(sublayout1.xml);

[html] view plaincopy

?xml version="1.0" encoding="utf-8"?

LinearLayout xmlns:android=""    

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#505050"

android:orientation="vertical" 

TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#ff0000"

android:text="第一个BTN" /

Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" One Button " /

/LinearLayout

2、再将标记有“第二部分”的代码段,分离成第二个文件(sublayout2.xml):

[html] view plaincopy

?xml version="1.0" encoding="utf-8"?

LinearLayout xmlns:android=""  

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" 

TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#00ff00"

android:text="第二个BTN" /

Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" Second Button " /

/LinearLayout

3、主文件中使用include,将上面两个文件包含进去(activity_main.xml);

[html] view plaincopy

?xml version="1.0" encoding="utf-8"?

LinearLayout xmlns:android=""    

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" 

include

android:id="@+id/main1"

layout="@layout/sublayout1" /

include

android:id="@+id/main2"

layout="@layout/sublayout2" /

Button

android:id="@+id/another"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" Another Button " /

/LinearLayout

这样就实现了相同的效果,这里可以看到,include并没有其它的功能,只是把一个XML布局引入进来当做自己的布局,跟直接把引用的这段代码写在include处的效果是一样的。

安卓如何将一个已经用JAVA代码改变了的控件加载到另外一个布局文件中

第一步:(在activityA中)

Intent mIntent = new Intent(A.this,B.class);     

Bundle mBundle = new Bundle();     

mBundle.putString("TEXTVIEWStr",TEXTVIEW.getText.toString);  //从你的TEXTVIEW控件上获取上面的字符串,然后放在inten里面传给activityB 

mIntent.putExtras(mBundle);  

startActivity(mIntent );   

第二步:获取值(在activityB中)

Bundle bundle = intent.getExtras();  

String name = bundle.getString("TEXTVIEWStr");

第三步:放置

Activity中的TEXTVIEW

TEXTVIEW_B.setText(name )

不知道是不是你要的效果

新闻名称:在java代码中加载布局 java代码加载顺序
标题URL:https://www.cdcxhl.com/article18/ddeiddp.html

成都网站建设公司_创新互联,为您提供标签优化微信小程序微信公众号面包屑导航外贸网站建设搜索引擎优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站建设