Android常用控件总结
1.Button:按钮单击事件
Button button = (Button) this.findViewById(R.id.imageButton);
// 设置图片按钮的背景
button.setBackgroundResource(R.drawable.icon01);
//设置背景颜色
//b.setBackgroundColor(color);
//b.setText(text);
// setOnClickListener() - 响应图片按钮的鼠标单击事件
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {//单击后的效果
TextView>
txt.setText("按钮被单击了");
}
});
2.EditText:编辑框
3.TextView:显示文本
4.ImageButton:图片按钮,用法和Button 相似
5.ImageView:图片显示控件
<!--ImageView - 图片显示控件 -->
<ImageView android:id="@+id/imageView" android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.imageview);
setTitle("ImageView");
ImageView>this.findViewById(R.id.imageView);
// 指定需要显示的图片
imgView.setBackgroundResource(R.drawable.icon01);
}
6.CheckBox:复选框
7.RadioButton:单选按钮
8. AnalogClock:钟表(带表盘的那种)控件
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub super.onCreate(savedInstanceState);
this.setContentView(R.layout.analogclcok)
setTitle("AnalogClock");
}
<!--AnalogClock - 钟表(带表盘的那种)控件-->
<AnalogClock android:id="@+id/analogClock"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</AnalogClock>
9. DigitalClock电子表控件
<!--DigitalClock - 电子表控件-->
<DigitalClock android:id="@+id/digitalClock"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</DigitalClock>
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.digitalclcok);
setTitle("DigitalClcok");
}
10. DatePicker日期选择控件
<!--DatePicker - 日期选择控件 -->
<DatePicker android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.datepicker);
// 具体的应用可参见对话框中的示例
setTitle("DatePicker");
}
11. TimePicker时间选择控件
<!--TimePicker - 时间选择控件-->
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TimePicker>
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.timepicker);
// 具体的应用可参见对话框中的示例
setTitle("TimePicker");
}
12.ToggleButton双状态按钮控件
<!-- ToggleButton - 双状态按钮控件
textOn - 当按钮状态为 true 时所显示的文本
textOff - 当按钮状态为 false 时所显示的文本
-->
<ToggleButton android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="关闭" android:textOff="打开" />
setTitle("ToggleButton");
final ToggleButton btn = (ToggleButton) this.findViewById(R.id.toggleButton);
// setOnClickListener() - 响应按钮的鼠标单击事件
btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
TextView txt = (TextView) _ToggleButton.this.findViewById(R.id.textView);
// ToggleButton.isChecked() - 双状态按钮的按钮状态
txt.setText("按钮状态:" + String.valueOf(btn.isChecked()));
}
});
13.ProgressBar进度条控件
// 设置特性以允许在应用程序的标题栏上显示进度条(条状) requestWindowFeature(Window.FEATURE_PROGRESS);
// 设置特性以允许在应用程序的标题栏上显示进度条(圆圈状)
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
this.setContentView(R.layout.progressbar);
setTitle("ProgressBar");
// 在标题栏上显示进度条(条状)
setProgressBarVisibility(true);
// 在标题栏上显示进度条(圆圈状)
setProgressBarIndeterminateVisibility(true);
// 指定进度条的进度
setProgress(50 * 100);
setSecondaryProgress(75 * 100);
<!--ProgressBar - 进度条控件 -->
<!--以下分别为大、中、小的进度条控件(圆圈状)-->
<ProgressBar android:id="@+android:id/progress_large"
style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar android:id="@+android:id/progress"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ProgressBar android:id="@+android:id/progress_small"
style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--
进度条控件(条状)的演示
style - 进度条的样式,本例使用内置样式
max - 进度的最大值
progress - 第一进度位置
secondaryProgress - 第二进度位置
-->
<ProgressBar android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal" android:layout_width="200px"
android:layout_height="wrap_content" android:max="100"
android:progress="50" android:secondaryProgress="75" />
14.SeekBar: 可拖动的进度条控件
<!--SeekBar - 可拖动的进度条控件
max - 进度的最大值
progress - 第一进度位置
secondaryProgress - 第二进度位置 -->
<SeekBar android:id="@+id/seekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:max="100"
android:progress="50" android:secondaryProgress="75" />
<TextView android:id="@+id/progress" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:id="@+id/tracking" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
public class _SeekBar extends Activity implements
SeekBar.OnSeekBarChangeListener {
SeekBar mSeekBar;
TextView mProgressText;
TextView mTrackingText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.seekbar);
setTitle("SeekBar");
mSeekBar = (SeekBar) findViewById(R.id.seekBar);
// setOnSeekBarChangeListener() - 响应拖动进度条事件
mSeekBar.setOnSeekBarChangeListener(this);
mProgressText = (TextView) findViewById(R.id.progress);
mTrackingText = (TextView) findViewById(R.id.tracking);
}
// 拖动进度条后,进度发生改变时的回调事件
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromTouch) {
mProgressText.setText(progress + "%");
}
// 拖动进度条前开始跟踪触摸
public void onStartTrackingTouch(SeekBar seekBar) {
mTrackingText.setText("开始跟踪触摸");
}
// 拖动进度条后停止跟踪触摸
public void onStopTrackingTouch(SeekBar seekBar) {
mTrackingText.setText("停止跟踪触摸");
}
}
15.AutoCompleteTextView -支持自动完成功能的可编辑文本控件
<AutoCompleteTextView
android:id="@+id/auto_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></AutoCompleteTextView>
<Button
android:id="@+id/btnname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_btnname"
></Button>
public class Text extends Activity {
AutoCompleteTextView autoview;
ArrayAdapter<String> adapter;
//default city
String[] city = {
"shanghai","beijing","tianjing","wuhan","sichuan"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intialView();
//default=2
autoview.setThreshold(1);
Button>btnname);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
autoview.getText().toString();
adapter.add(string);
}
});
}
public void intialView(){
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, city);
autoview = (AutoCompleteTextView) findViewById(R.id.auto_edit);
autoview.setWidth(200);
autoview.setAdapter(adapter);
}
}
16.MultiAutoCompleteTextView: 支持自动完成功能的可编辑文本控件
<!--MultiAutoCompleteTextView
- 支持自动完成功能的可编辑文本控件,允许输入多值(多值之间会自动地用指定的分隔符分开)
-->
<MultiAutoCompleteTextView android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
public class _MultiAutoCompleteTextView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multiautocompletetextview);
setTitle("MultiAutoCompleteTextView");
// 实例化适配器,指定显示格式及数据源
ArrayAdapter<String>>new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line,
ary);
MultiAutoCompleteTextView>editText);
textView.setAdapter(adapter);
// 设置多个值之间的分隔符,此处为逗号
textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
// 自动完成控件的所需数据的数据源
private String[] ary = new String[] {
"abc","abcd","abcde","abcdef","abcdefg",
"hij","hijk","hijkl","hijklm","hijklmn", };
}
17.ZoomControls:放大/缩小按钮控件
<!--放大/缩小按钮控件-->
<ZoomControls android:id="@+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
public class _ZoomControls extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.zoomcontrols);
setTitle("ZoomControls");
ZoomControls>this.findViewById(R.id.zoomControls);
// setOnZoomInClickListener() - 响应单击放大按钮的事件
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(_ZoomControls.this, "单击了放大按钮", Toast.LENGTH_SHORT).show();
}
});
// setOnZoomOutClickListener() - 响应单击缩小按钮的事件
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(_ZoomControls.this, "单击了缩小按钮", Toast.LENGTH_SHORT).show();
}
});
}}
18.Include将指定的 layout 整合进来
<!--
include - 将指定的 layout 整合进来
layout - 指定需要整合 layout(include_2.xml文件)
-->
<include android:id="@+id/cell1" layout="@layout/include_1" />
<include android:id="@+id/cell2"
android:layout_width="fill_parent"
layout="@layout/include_2" />
this.setContentView(R.layout.include);
19. VideoView:视频播放控件
<!--VideoView - 视频播放控件 -->
<VideoView android:id="@+id/videoView" android:layout_width="wrap_content"
android:layout_height="wrap_content"> </VideoView>
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.videoview);
setTitle("VideoView");
VideoView>videoView);
// 指定需要播放的视频的地址 videoView.setVideoURI(Uri.parse("android.resource://com.webabcd.view/" + R.raw.demo));
// videoView.setVideoPath();
// 设置播放器的控制条
videoView.setMediaController(new MediaController(this));
// 开始播放视频
videoView.start();
}
20.WebView浏览器控件
<!--WebView - 浏览器控件(WebKit 内核)-->
<WebView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/webView" />
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.webview);
setTitle("WebView");
WebView>webView);
// 配置浏览器,使其可支持 JavaScript
WebSettings>
webSettings.setJavaScriptEnabled(true);
// 清除浏览器缓存
webView.clearCache(true);
// 指定浏览器需要解析的 url 地址
webView.loadUrl("");
// 指定浏览器需要解析的 html 数据
// webView.loadData("<a href=''>webabcd</a>", "text/html", "utf-8");
}
21.RatingBar评分控件
<!--RatingBar - 评分控件
numStars - 评分控件的星星的数量
rating - 当前评分的值-->
<RatingBar android:id="@+id/ratingBar" android:numStars="5"android:rating="1.5"
android:layout_width="wrap_content"android:layout_height="wrap_content" />
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
public class _RatingBar extends Activity implements RatingBar.OnRatingBarChangeListener {
private RatingBar mRatingBar;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.ratingbar);
setTitle("RatingBar");
mTextView = (TextView) findViewById(R.id.textView);
mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
// setOnRatingBarChangeListener() - 响应评分值发生改变的事件
mRatingBar.setOnRatingBarChangeListener(this);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
mTextView.setText(String.valueOf(rating));
}
}
21.Tab:选项卡:三种实现方式
public class MainTest extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("SMS")
.setContent(new Intent(this, SMSTest.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("联系人").setContent(new Intent(this, LinkManTest.class)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("个人信息").setContent(new Intent(this, InfoTest.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
}
public class Tabs2 extends TabActivity implements TabHost.TabContentFactory {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on))
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("tab2")
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(this));
}
/** {@inheritDoc} */
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return tv;
}
}
public class Tabs1 extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost>
LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(R.id.view3));
}
}
22.Spinner-下拉框控件与setDropDownViewResource
<!--Spinner - 下拉框控件-->
<Spinner android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
public class _Spinner extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.spinner);
setTitle("Spinner");
Spinner>spinner);
// 设置下拉框控件的标题文本
spinner.setPrompt("请选择");
// 实例化适配器,指定显示格式及数据源
ArrayAdapter<CharSequence>>createFromResource(
this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
// setOnItemSelectedListener() - 响应下拉框的选中值发生变化的事件
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView>this.findViewById(R.id.textView);
textView.setText(((TextView)arg1).getText());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}}
23.Chronometer计时器控件
<!--Chronometer - 计时器控件 -->
<Chronometer android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始计时">
<requestFocus /> </Button>
<Button android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止计时" />
<Button android:id="@+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计时器复位" />
public class _Chronometer extends Activity {
private Chronometer mChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.chronometer);
setTitle("Chronometer");
Button button;
mChronometer = (Chronometer) findViewById(R.id.chronometer);
// 设置计时器所显示的时间格式
mChronometer.setFormat("计时:(%s)");
button = (Button) findViewById(R.id.btnStart);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.btnStop);
button.setOnClickListener(mStopListener);
button = (Button) findViewById(R.id.btnReset);
button.setOnClickListener(mResetListener);
}
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
// 启动计时器
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
// 暂停计时器
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
// 复位计时器,即停止计时器
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};}
24.ScrollView滚动条控件
<!-- ScrollView - 滚动条控件 scrollbarStyle - 滚动条的样式 -->
<ScrollView android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="100px"
android:scrollbarStyle="outsideOverlay"
android:background="@android:drawable/edit_text">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/textView" />
</ScrollView>
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.scrollview);
setTitle("ScrollView");
TextView textView = (TextView)this.findViewById(R.id.textView);
textView.setText("a\na\na\na\na\na\na\na\na\na\na\na\na"
+"\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na");
}
25.TextSwitcher- 文字转换器控件
<Button android:id="@+id/btnChange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="改变文字" />
<!--TextSwitcher - 文字转换器控件(改变文字时增加一些动画效果) -->
<TextSwitcher ndroid:id="@+id/textSwitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
public class _TextSwitcher extends Activity implements ViewSwitcher.ViewFactory {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.textswithcer);
setTitle("TextSwithcer");
final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
// 指定转换器的 ViewSwitcher.ViewFactory
switcher.setFactory(this);
// 设置淡入和淡出的动画效果
Animation>loadAnimation(this, android.R.anim.fade_in);
Animation>loadAnimation(this, android.R.anim.fade_out);
switcher.setInAnimation(in);
switcher.setOutAnimation(out);
// 单击一次按钮改变一次文字
Button>this.findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switcher.setText(String.valueOf(new Random().nextInt()));
}
});
}
// 重写 ViewSwitcher.ViewFactory 的 makeView(),返回一个 View
@Override
public View makeView() {
TextView>new TextView(this);
textView.setTextSize(36);
return textView;
}
}
26.ListView
27.Gallery缩略图浏览器控件
<!--Gallery - 缩略图浏览器控件
spacing - 缩略图列表中各个缩略图之间的间距-->
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:spacing="20px" />
public class _Gallery extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.gallery);
setTitle("Gallery");
Gallery>gallery);
// 为缩略图浏览器指定一个适配器
gallery.setAdapter(new ImageAdapter(this));
// 响应 在缩略图列表上选中某个缩略图后的 事件
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(_Gallery.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
// 继承 BaseAdapter 用以实现自定义的图片适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView>new ImageView(mContext);
image.setImageResource(mThumbIds[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}
}
// 需要显示的图片集合
private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
}
28.ImageSwitcher
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="20px" />
<!--
ImageSwitcher -
图片转换器控件(改变图片时增加一些动画效果)
-->
<ImageSwitcher android:id="@+id/imageSwitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
public class ImageSwitcherTest extends Activity implements ViewFactory {
ImageSwitcher imageswitcher;
public final Integer[] imagesstr = { R.drawable.p1, R.drawable.p2, R.drawable.p3,R.drawable.p4, R.drawable.p5 };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageswitcher);
setTitle("ImageSwitcher");
final ImageSwitcher imgs = (ImageSwitcher) findViewById(R.id.imageSwitcher);
imgs.setFactory(this);
imgs.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imgs.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery>gallery02);
gallery.setAdapter(new ImageAdapter(this));
gallery
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
imgs.setImageResource(imagesstr[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView>new ImageView(this);
image.setMinimumHeight(200);
image.setMinimumWidth(200);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return image;
}
public class ImageAdapter extends BaseAdapter {
public Context mcontext;
public ImageAdapter(Context context) {
mcontext = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imagesstr.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 convertView, ViewGroup parent) {
ImageView>new ImageView(mcontext);
imgv.setImageResource(imagesstr[position]);
imgv.setAdjustViewBounds(true);
imgv.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return imgv;
}}}
29.GridView
<!--
GridView - 网格控件
numColumns="auto_fit" - 列数自适应
stretchMode - 缩放模式(stretchMode="columnWidth" - 缩放与列宽大小同步)
-->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10px"
android:verticalSpacing="10px"
android:horizontalSpacing="10px"
android:numColumns="auto_fit"
android:columnWidth="60px"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
public class _GridView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.gridview);
setTitle("GridView");
GridView>gridView);
// 指定网格控件的适配器为自定义的图片适配器
gridView.setAdapter(new ImageAdapter(this));
}
// 自定义的图片适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/*
* 将图片放入ImageView(non-Javadoc) *
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(48, 48));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// 网格控件所需图片数据的数据源
private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
}}