UI组件的综合运用
一、
1、activity_main.xml
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/IB1"
android:layout_width="70dp"
android:layout_height="70dp"
android:onClick="click"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
2、xuanze.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/Ga1"
android:layout_width="fill_parent"
android:spacing="10dp"
android:layout_height="90dp" />
<ImageSwitcher
android:id="@+id/IS1"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true">
</ImageSwitcher>
</RelativeLayout>
3、MainActivity.java
package com.example.ui8;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
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.p1, R.drawable.p2,
R.drawable.p3, R.drawable.p4, R.drawable.p5,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.IB1);
imageButton.setBackgroundResource(R.drawable.p1);
}
public void click(View v) {
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View view = layoutInflater.inflate(R.layout.xuanze, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
imageSwitcher = (ImageSwitcher) view.findViewById(R.id.IS1);
gallery = (Gallery) view.findViewById(R.id.Ga1);
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.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
@Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView(this);
}
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;
}
}
}
二、运行效果图
点击头像选择图片: