步驟:
公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出防城港免費做網(wǎng)站回饋大家。
1、在存放使用資源的res文件夾下的layout文件夾內(nèi)新建一個XML布局文件,如命名為:page1.xml。
2、在存放資、代碼的文件夾下下找到MainActivity.java,雙擊打開,在onCreate的方法內(nèi)添加關(guān)聯(lián)代碼。
Android在xml文件中可使用include包含其他定義好的布局, 可以將多處用到的布局單獨出來,然后用include包含進來,這種包含方法相當(dāng)于把原來布局的一部分代碼獨立出來,供大家共同使用,也就相當(dāng)于面向?qū)ο蛑械念惖母拍畈畈欢唷O旅嫖覀冎鸩街v解include的作用。
先看下我們要實現(xiàn)的整體界面:
一、未使用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、先將上面代碼標(biāo)記有“第一部分”的,代碼段分離成一個文件(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、再將標(biāo)記有“第二部分”的代碼段,分離成第二個文件(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
這樣就實現(xiàn)了相同的效果,這里可以看到,include并沒有其它的功能,只是把一個XML布局引入進來當(dāng)做自己的布局,跟直接把引用的這段代碼寫在include處的效果是一樣的。
第一步:(在activityA中)
Intent?mIntent?=?new?Intent(A.this,B.class);?????
Bundle?mBundle?=?new?Bundle();?????
mBundle.putString("TEXTVIEWStr",TEXTVIEW.getText.toString);??//從你的TEXTVIEW控件上獲取上面的字符串,然后放在inten里面?zhèn)鹘oactivityB?
mIntent.putExtras(mBundle);??
startActivity(mIntent?);???
第二步:獲取值(在activityB中)
Bundle?bundle?=?intent.getExtras();??
String?name?=?bundle.getString("TEXTVIEWStr");
第三步:放置
Activity中的TEXTVIEW
TEXTVIEW_B.setText(name?)
不知道是不是你要的效果
新聞名稱:在java代碼中加載布局 java代碼加載順序
標(biāo)題URL:http://chinadenli.net/article18/ddeiddp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、微信小程序、微信公眾號、面包屑導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)