学习Android之第七个小程序头像选择(自定义对话框、Gallery、ImageSwitcher)

效果图如下:

          


一共一个activity和两个xml。

******当我们需要使用的组件不在setContentView()设置的布局文件中,那我们就需要使用inflate()方法来获取,使用view对象调用findViewByid(),作者一开直接调用findViewByid,走了好多弯路,于是写此博文来帮助各位博友,知道了就很简单了。*******


MainActivity.java

package com.example.head;



import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
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.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements ViewFactory{
	
	
	ImageButton imageButton ;
	Gallery gallery;
	ImageSwitcher imageSwitcher;
	
	int index;
	
	private final static Integer[] IMAGES = {
		R.drawable.z1,
		R.drawable.z2,
		R.drawable.z3
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		imageButton = (ImageButton)findViewById(R.id.IB01);
		
		imageButton.setBackgroundResource(R.drawable.z1);
		
		
	}
	
	public void click(View v){
		

		LayoutInflater layoutInflater =LayoutInflater.from(MainActivity.this);
		View view = layoutInflater.inflate(R.layout.headchoose, null);
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

		imageSwitcher= (ImageSwitcher)view.findViewById(R.id.IS01);
		 //组件不在activity_main,所以先要使用inflate获得headchoose布局
		//并赋值给view对象,使用view对象,来调用findViewByid()方法,如果不用view来
		//调用findViewByid(),找不到会报错。
		
		gallery =  (Gallery)view.findViewById(R.id.Gall01);
		
		imageSwitcher.setFactory(MainActivity.this);
	
		gallery.setAdapter(new ImageAdapter(MainActivity.this));
		
		
		
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				imageSwitcher.setImageResource(IMAGES[position]);
				index = position;
				
			}
		});
		
		
		
		builder.setTitle("图片选择")
		.setView(view)
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				imageButton.setBackgroundResource(IMAGES[index]);
				
			}
	
		});		
		builder.show();
		
		
		
	}



	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		return new ImageView(this);
	}

	
	/*
	 * ImageAdapter
	 * 
	 */
	
	class ImageAdapter extends BaseAdapter{
		
		Context context;
		
		public ImageAdapter(Context context){
			this.context = context;
			
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return IMAGES.length;
		}

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

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

		@Override
		public View getView(int position, View v, ViewGroup vg) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(IMAGES[position]);
			imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

			return imageView;
		}
	}

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <ImageButton
        android:id="@+id/IB01" 
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:onClick="click"/>
    
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

headchoose.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Gallery
        android:id="@+id/Gall01"
        android:layout_width="fill_parent"
        android:spacing="10dp"
        android:layout_height="90dp" />

    <ImageSwitcher
        android:id="@+id/IS01"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_centerHorizontal="true">
    </ImageSwitcher>

</RelativeLayout>















学习Android之第七个小程序头像选择(自定义对话框、Gallery、ImageSwitcher),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。