以一個簡單的圖片瀏覽器實例說明
成都創(chuàng)新互聯(lián)公司是專業(yè)的山陽網(wǎng)站建設(shè)公司,山陽接單;提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計、成都外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行山陽網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
新建工程后:
在activit_main.xml中添加代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
在java中添加代碼:
public class MainActivity extends Activity { int[] p_w_picpath = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4 }; ImageView myp_w_picpath; int curImage = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout layout = (LinearLayout)this.findViewById(R.id.main); myp_w_picpath = new ImageView(this); layout.addView(myp_w_picpath); myp_w_picpath.setImageResource(p_w_picpath[0]); myp_w_picpath.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub myp_w_picpath.setImageResource(p_w_picpath[++curImage%p_w_picpath.length]); } }); }
在Android中,每一個組件的性質(zhì)可以在.xml中定義,同時每個性質(zhì)都有對應(yīng)的java方法
注意:以上代碼中 使用java代碼添加新的組件,可把聲明寫在類中,但是他必須被賦值為一個new在onCreate()內(nèi)部的對象,否則程序會出錯,究其原因是因為在java中添加組件的時候必須初始化的時候加入?yún)?shù)(this)
當前文章:Android使用xml和java混合編寫ui
網(wǎng)頁URL:http://aaarwkj.com/article48/iijshp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站營銷、企業(yè)建站、網(wǎng)頁設(shè)計公司、面包屑導航、品牌網(wǎng)站制作
聲明:本網(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)