首頁 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當(dāng)貝市場

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網(wǎng)站活動 | Z幣中心

新手入門 / 進階 / 社區(qū)互助

新手 | 你問我答 | 免費刷機救磚 | ROM固件

查看: 12502|回復(fù): 0
上一主題 下一主題
[教程]

第十三講:用戶界面 View(八)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:19 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
679
本講內(nèi)容:Gallery,GridView
十五、Gallery 畫廊
Gallery是一個內(nèi)部元素可以水平滾動,并且可以把當(dāng)前選擇的子元素定位在它中心的布局組件。
我們還是直接看看例子的運行效果。
  
下面上代碼,相關(guān)解釋都放在代碼里了。
1、建立一個新項目 HelloGallery
2、拷貝wallpaper_0.jpg…wallpaper_9.jpg 10個圖片文件到res/drawable目錄
3、res/layout/main.xml文件的內(nèi)容如下:
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <framelayout android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01">   
    <imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/ImageView01" android:src="@drawable/wallpaper_0">   
    </imageview>   
       
    <gallery android:layout_height="wrap_content" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Gallery01" android:spacing="5dp">   
    </gallery>   
    </framelayout>
復(fù)制代碼
其中我們使用FrameLayout來實現(xiàn)疊加效果,使用ImageView來顯示大圖,Gallery來展示畫廊,android:spacing="5dp" 屬性則是用來設(shè)置元素之間的間隔。4、在res/values/目錄中新建一個attrs.xml內(nèi)容如下:   
  1. <?xml version="1.0" encoding="UTF-8"?>   
    <resources>   
            <declare -styleable="" name="HelloGallery">   
                    <attr name="android:galleryItemBackground">   
            </attr></declare>   
    </resources>
復(fù)制代碼
5、在MainHelloGallery.java中的內(nèi)容如下:   
  1. package android.basic.lesson13;   
       
    import android.app.Activity;   
    import android.content.Context;   
    import android.content.res.TypedArray;   
    import android.os.Bundle;   
    import android.view.View;   
    import android.view.ViewGroup;   
       
    import android.widget.AdapterView;   
    import android.widget.AdapterView.OnItemClickListener;   
    import android.widget.BaseAdapter;   
    import android.widget.Gallery;   
    import android.widget.ImageView;   
    import android.widget.Toast;   
       
    public class MainHelloGallery extends Activity {   
       
            /** Called when the activity is first created. */   
            @Override   
            public void onCreate(Bundle savedInstanceState) {   
                    super.onCreate(savedInstanceState);   
                    setContentView(R.layout.main);   
       
                    //定義UI組件   
                    final ImageView iv= (ImageView)findViewById(R.id.ImageView01);   
                    Gallery g = (Gallery) findViewById(R.id.Gallery01);   
       
                    //設(shè)置圖片匹配器   
                    g.setAdapter(new ImageAdapter(this));   
       
                    //設(shè)置AdapterView點擊監(jiān)聽器,Gallery是AdapterView的子類   
                    g.setOnItemClickListener(new OnItemClickListener() {   
       
                            @Override   
                            public void onItemClick(AdapterView<?> parent, View view,   
                                            int position, long id) {   
                                    //顯示點擊的是第幾張圖片   
                                    Toast.makeText(MainHelloGallery.this, "" + position,   
                                                    Toast.LENGTH_LONG).show();   
                                    //設(shè)置背景部分的ImageView顯示當(dāng)前Item的圖片   
                                    iv.setImageResource(((ImageView)view).getId());   
                            }   
                    });   
            }   
       
            //定義繼承BaseAdapter的匹配器   
            public class ImageAdapter extends BaseAdapter {   
       
                    //Item的修飾背景   
                    int mGalleryItemBackground;   
       
                    //上下文對象   
                    private Context mContext;   
       
                    //圖片數(shù)組   
                    private Integer[] mImageIds = { R.drawable.wallpaper_0,   
                                    R.drawable.wallpaper_1, R.drawable.wallpaper_2,   
                                    R.drawable.wallpaper_3, R.drawable.wallpaper_4,   
                                    R.drawable.wallpaper_5, R.drawable.wallpaper_6,   
                                    R.drawable.wallpaper_7, R.drawable.wallpaper_8,   
                                    R.drawable.wallpaper_9 };   
       
                    //構(gòu)造方法   
                    public ImageAdapter(Context c){   
                            mContext = c;   
                            //讀取styleable資源   
                    TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);   
                    mGalleryItemBackground = a.getResourceId(   
                            R.styleable.HelloGallery_android_galleryItemBackground, 0);   
                    a.recycle();   
       
                    }   
       
                    //返回項目數(shù)量   
                    @Override   
                    public int getCount() {   
                            return mImageIds.length;   
                    }   
       
                    //返回項目   
                    @Override   
                    public Object getItem(int position) {   
                            return position;   
                    }   
       
                    //返回項目Id   
                    @Override   
                    public long getItemId(int position) {   
                            return position;   
                    }   
       
                    //返回視圖   
                    @Override   
                    public View getView(int position, View convertView, ViewGroup parent) {   
       
                            ImageView iv = new ImageView(mContext);   
                            iv.setImageResource(mImageIds[position]);   
                            //給生成的ImageView設(shè)置Id,不設(shè)置的話Id都是-1   
                            iv.setId(mImageIds[position]);   
                            iv.setLayoutParams(new Gallery.LayoutParams(120, 160));   
                            iv.setScaleType(ImageView.ScaleType.FIT_XY);   
                            iv.setBackgroundResource(mGalleryItemBackground);   
                            return iv;   
                    }   
       
            }   
    }
復(fù)制代碼
  
   我們點擊某一張圖片,會把該子元素的圖片顯示在放在后面一層的ImageView組件中。有興趣的同學(xué)可以了解一下AdapterView的繼承關(guān)系:  十六、GridView 網(wǎng)格組件(待續(xù))   

上一篇:第十四講:Service入門指南
下一篇:第十一講:用戶界面 View(六)
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

Archiver|新帖|標(biāo)簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號 )

網(wǎng)絡(luò)信息服務(wù)信用承諾書 | 增值電信業(yè)務(wù)經(jīng)營許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號

GMT+8, 2024-10-20 21:02 , Processed in 0.066322 second(s), 13 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表