Android[初級教程] 第九章 Gallery控制項和ImageSwitcher控制項

來源:互聯網
上載者:User

這一章我們介紹Gallery控制項和ImageSwitcher控制項,這次妖怪們說,現在iphone很流行,上次的通輯令不好看,沒有互動,有沒有好看一點的通輯令呢?呵呵,這次我們就綜合兩個控制項來做個好看的通輯令.先吧


 

OK,我們先上main.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:text="西遊記各主人公通輯令" android:id="@+id/textView1" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
    <ImageSwitcher android:id="@+id/imageSwitcher" 
        android:layout_width="320dp" android:layout_height="280dp"></ImageSwitcher> 
    <Gallery android:layout_width="match_parent" 
        android:layout_height="wrap_content" android:id="@+id/gallery1"></Gallery> 
    <TextView android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:text="@string/hello" 
        android:id="@+id/text"></TextView> 
</LinearLayout> 
主Layout中定義了一個ImageSwitcher和一個Gallery控制項

再定義了一個gallery_item.xml的Layout,是Gallery控制項中既有圖又有字的Layout

 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView android:id="@+id/item_imageView" android:src="@drawable/bajie" 
        android:layout_gravity="center" android:scaleType="fitXY" 
        android:layout_marginLeft="30dp" android:layout_marginRight="30dp" 
        android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView> 
    <TextView android:text="TextView" android:id="@+id/item_textView" 
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:layout_gravity="center" android:layout_marginLeft="30dp" 
        android:layout_marginRight="30dp"></TextView> 
 
</LinearLayout> 
好,現在上主要的java代碼:www.2cto.com
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.ViewSwitcher.ViewFactory; 
 
public class ButtonDemoActivity extends Activity 

    private TextView text = null; 
    private int[] image = { R.drawable.tangseng, R.drawable.wukong, 
            R.drawable.bajie, R.drawable.shaseng }; 
    private String[] item = { "唐僧", "孫悟空 ", "豬八戒", "沙和尚" }; 
    private Gallery gallery; 
    private ImageSwitcher switcher; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        // 通過ID尋找到main.xml中的TextView控制項 
        text = (TextView) findViewById(R.id.text); 
 
        // 通過ID尋找到main.xml中的ImageSwitcher控制項 
        switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); 
 
        switcher.setFactory(new ViewFactory() 
        { 
 
            // 建立ImageSwitcher中的視圖 
            @Override 
            public View makeView() 
            { 
                ImageView imageView = new ImageView(ButtonDemoActivity.this); 
                imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
                imageView.setPadding(10, 10, 10, 10); 
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams( 
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
                return imageView; 
            } 
        }); 
 
        // 通過ID尋找到main.xml中的Gallery控制項 
        gallery = (Gallery) findViewById(R.id.gallery1); 
 
        // 設定Gallery適配器 
        BaseAdapter adapter = new BaseAdapter() 
        { 
 
            // 取得適配器中的視圖 
            @Override 
            public View getView(int position, View convertView, ViewGroup parent) 
            { 
                //將適配器中緩衝的視圖控制項返回 
                View view = convertView; 
 
                if (view == null) 
                { 
                    // 將gallery_item.xml適配到View中 
                    LayoutInflater inflater = LayoutInflater 
                            .from(getApplicationContext()); 
                    view = inflater.inflate(R.layout.gallery_item, null); 
                } 
 
                // 尋找gallery_item.xml中的ImageView控制項 
                ImageView imageView = (ImageView) view 
                        .findViewById(R.id.item_imageView); 
                imageView.setImageResource(image[position]); 
 
                // 尋找gallery_item.xml中的TextView控制項 
                TextView textView = (TextView) view 
                        .findViewById(R.id.item_textView); 
                textView.setText(item[position]); 
                return view; 
            } 
 
            @Override 
            public long getItemId(int position) 
            { 
 
                return position; 
            } 
 
            @Override 
            public Object getItem(int position) 
            { 
                return position; 
            } 
 
            @Override 
            public int getCount() 
            { 
 
                return image.length; 
            } 
        }; 
 
        gallery.setAdapter(adapter); 
 
        // 設定Gallery單項選擇監聽器 
        gallery.setOnItemSelectedListener(new OnItemSelectedListener() 
        { 
 
            @Override 
            public void onItemSelected(AdapterView<?> arg0, View arg1, 
                    int position, long arg3) 
            { 
                String str = "你現在查看的是" + item[position]; 
                updateText(str); 
                switcher.setImageResource(image[position]); 
            } 
 
            @Override 
            public void onNothingSelected(AdapterView<?> arg0) 
            { 
                // TODO Auto-generated method stub 
 
            } 
        }); 
 
    } 
 
    private void updateText(String string) 
    { 
        // 將文本資訊設定給TextView控制項顯示出來 
        text.setText(string); 
    } 
 

呵呵,這下妖怪們的建議滿足了,妖怪們開心都抓唐僧去了.好了,這一章也結束了,謝謝

摘自:kangkangz4的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.