头像选择

1.进行主界面的布局activity_main.xml

<RelativeLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageButton1"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@+id/imageButton1"
        android:ems="10" >

        <requestFocus />
    </EditText>
  
</RelativeLayout>

2.进行对话框中组件的布局activity_show_gallery.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
     <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="?android:galleryItemBackground"
        android:gravity="center_vertical"
        android:spacing="3dp"
      
        />

   

</RelativeLayout>

3.在MainActivity中查找组件进行相应的处理

package cn.edu.bzu.headchoice;





import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.res.TypedArray;




import android.view.LayoutInflater;


import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;






import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;




import android.widget.ImageButton;

import android.widget.ImageView;




public class MainActivity extends Activity {
	private ImageButton imageButton1;
	private Gallery gallery;
	
	
	private int[] pics = { R.drawable.a, R.drawable.b, R.drawable.c,
			R.drawable.d, R.drawable.e};
	
	int pos=pics.length/2;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		imageButton1=(ImageButton) this.findViewById(R.id.imageButton1);
		
		imageButton1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				AlertDialog.Builder alertDialog=new AlertDialog.Builder(MainActivity.this);
				
				LayoutInflater layoutInflater=getLayoutInflater();
				View view1=layoutInflater.inflate(R.layout.activity_show_gallery,null);
				
				
				alertDialog.setTitle("请选择头像");
				alertDialog.setView(view1);
				//下面这句话最为重要
				gallery=(Gallery) view1.findViewById(R.id.gallery1);
				
				//设置默认选择的图片
				gallery.setSelection(pics[2]);
				
				gallery.setAdapter(new ImageAdapter());
				gallery.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> adapter, View view,
							int position, long id) {
						pos=position;
						
						
					}
				});
				
					
				
				alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						imageButton1.setImageResource(pics[pos]);
						
					}
				});
			alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
					
				}
			});
			alertDialog.create().show();
			}
		});
	}
	private class ImageAdapter extends BaseAdapter{
		int mGalleryItemBackground;;

		public ImageAdapter() {
			TypedArray typedArray = obtainStyledAttributes(R.styleable.HelloGallery);
			mGalleryItemBackground = typedArray.getResourceId(
					R.styleable.HelloGallery_android_galleryItemBackground, 0);
			typedArray.recycle();
		}

		@Override
		public int getCount() {
			
			return pics.length;
		}

		@Override
		public Object getItem(int position) {
			
			return position;
		}

		@Override
		public long getItemId(int position) {
			
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			ImageView img=new ImageView(MainActivity.this);
			
			//设置图片资源
			img.setImageResource(pics[position]);
			 img.setScaleType(ImageView.ScaleType.FIT_XY);

			
			
			//设置图片大小
			
			img.setLayoutParams(new Gallery.LayoutParams(72, 72));
			img.setBackgroundResource(mGalleryItemBackground);
			return img;
		}
		
	}

}
					
	

4.运行效果



 


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值