安卓app_sl4_8在Eclipse中创建Android项目,名称为4.8,实现在屏幕中添加画廊视图,用于浏览图片

安卓app_sl4_8在Eclipse中创建Android项目,名称为4.8,实现在屏幕中添加画廊视图,用于浏览图片

package com.example.sl4_8;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 MainActivity extends Activity {
    private int[] imageId=new int[]{R.drawable.img01,
            R.drawable.img02,
            R.drawable.img03,
            R.drawable.img04,
            R.drawable.img05,
            R.drawable.img06,
            R.drawable.img07,
            R.drawable.img08,
            R.drawable.img09,
            R.drawable.img10,
            R.drawable.img11,
            R.drawable.img12};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Gallery gallery=(Gallery)findViewById(R.id.gallery1);
        BaseAdapter adapter = new BaseAdapter()
                {

                    @Override
                    public int getCount() {
                        // TODO Auto-generated method stub
                        //return 0;
                        return imageId.length;
                    }

                    @Override
                    public Object getItem(int position) {
                        // TODO Auto-generated method stub
                        //return null;
                        return position;
                    }

                    @Override
                    public long getItemId(int position) {
                        // TODO Auto-generated method stub
                        //return 0;
                        return position;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        // TODO Auto-generated method stub
                        ImageView imageview;
                        if(convertView ==null)
                        {
                            imageview=new ImageView(MainActivity.this);
                            imageview.setScaleType(ImageView.ScaleType.FIT_XY);
                            imageview.setLayoutParams(new Gallery.LayoutParams(180,135));
                            TypedArray typedArray=obtainStyledAttributes(R.styleable.Gallery);
                            imageview.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));
                            imageview.setPadding(5, 0, 5, 0);
                        }
                        else
                        {
                            imageview=(ImageView) convertView;
                        }
                        imageview.setImageResource(imageId[position]);
                        return imageview;
                    }
            
                };
                gallery.setAdapter(adapter);
                gallery.setSelection(imageId.length/2);
                gallery.setOnItemClickListener(new OnItemClickListener(){

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, "您选择了第"+String.valueOf(position), Toast.LENGTH_SHORT).show();
                        
                    }
                    
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    tools:context="com.example.sl4_8.MainActivity" >

    <Gallery
        android:id="@+id/gallery1"
        android:spacing="5dp"
        android:unselectedAlpha="0.6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

 

posted @ 2022-03-07 12:51  txwtech  阅读(74)  评论(0编辑  收藏  举报