zoukankan      html  css  js  c++  java
  • 得知Android小遴选程序第七头(他们定义对话框、Gallery、ImageSwitcher)

    效果如下面的:

              


    一共一个activity和两个xml。

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

    *******


    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>















    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    HTML初体验
    out传值
    函数
    冒泡排序
    数组
    异常语句
    类 string math
    for 穷举 迭代
    HTML JavaScript及运算符
    HTML 格式与布局
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4882570.html
Copyright © 2011-2022 走看看