探索Gallery和ImageSwitcher布局
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 > 6 <Gallery 7 android:id="@+id/img_gallery" 8 android:layout_width="fill_parent" 9 android:layout_height="110px" 10 android:layout_marginTop = "100px" 11 android:layout_alignParentLeft="true" 12 /> 13 <ImageSwitcher 14 android:id="@+id/image_switcher" 15 android:layout_width="90px" 16 android:layout_height="90px" 17 android:layout_alignParentTop="true" 18 android:layout_centerHorizontal="true" 19 android:layout_alignBottom="@+id/img_gallery" 20 21 ></ImageSwitcher> 22 </RelativeLayout>
当把上面Gallery一项属性改成android:layout_marginTop = "0px",出现下面的效果
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 > 6 <Gallery 7 android:id="@+id/img_gallery" 8 android:layout_width="fill_parent" 9 android:layout_height="110px" 10 android:layout_marginTop = "0px" 11 android:layout_alignParentLeft="true" 12 /> 13 <ImageSwitcher 14 android:id="@+id/image_switcher" 15 android:layout_width="90px" 16 android:layout_height="90px" 17 18 android:layout_centerHorizontal="true" 19 android:layout_alignBottom="@+id/img_gallery" 20 21 ></ImageSwitcher> 22 </RelativeLayout>
删掉其中一行
PS: iv.setLayoutParams(new Gallery.LayoutParams(80, 80)); iv.setPadding(15, 10, 15, 10);//setPadding(int left, int top, int right, int bottom)
iv.setLayoutParams(new ImageSwitcher.LayoutParams(90,90));
Gallery中的图像居中显示,单个的图像的实际大小为60*60,虽然代码中指定为80*80,但还需要减去上下Padding,背景画布的高度为110,在xml中指定。
若想整体放大,画布高度调整为300,选中图片大小为160*160,队列图片大小为130*130。且选中图片刚好居中覆盖后面的图片。
75
10
130 160 则Switcher距离最下面的高度应该为 75+10- (160-130)/2 = 70,这个70应该设置为Switcher的layout_marginBottom
10
75
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 > 6 <Gallery 7 android:id="@+id/img_gallery" 8 android:layout_width="fill_parent" 9 android:layout_height="300px" 10 android:layout_marginTop = "0px" 11 android:layout_alignParentLeft="true" 12 /> 13 <ImageSwitcher 14 android:id="@+id/image_switcher" 15 android:layout_width="160px" 16 android:layout_height="160px" 17 android:layout_marginBottom = "70px" 18 android:layout_centerHorizontal="true" 19 android:layout_alignBottom="@+id/img_gallery" 20 21 ></ImageSwitcher> 22 </RelativeLayout>
达到预期效果:
完整控制代码:
package com.hixin.contact; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.Gallery.LayoutParams; import android.widget.HorizontalScrollView; import android.widget.ImageButton; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { ImageButton btn_img; AlertDialog imageChooseDialog; Gallery gallery; ImageSwitcher is; private int[] images ={ R.drawable.image1,R.drawable.image2,R.drawable.image3, R.drawable.image4,R.drawable.image5,R.drawable.image6, R.drawable.image7,R.drawable.image8,R.drawable.image9, R.drawable.image10,R.drawable.image11,R.drawable.image12, R.drawable.image13,R.drawable.image14,R.drawable.image15, R.drawable.image16,R.drawable.image17,R.drawable.image18, R.drawable.image19,R.drawable.image20,R.drawable.image21, R.drawable.image22,R.drawable.image23,R.drawable.image24, R.drawable.image25,R.drawable.image26,R.drawable.image27, R.drawable.image28,R.drawable.image29,R.drawable.image30}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.addnew); btn_img = (ImageButton) this.findViewById(R.id.btn_img); btn_img.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { initImageChooseDialog(); imageChooseDialog.show(); } }); } protected void initImageChooseDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择头像"); LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.imageswitch,null); gallery = (Gallery) view.findViewById(R.id.img_gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setSelection(images.length/2); gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { is.setImageResource(images[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); is = (ImageSwitcher) view.findViewById(R.id.image_switcher); is.setFactory(new MyViewFactory(this)); builder.setView(view); imageChooseDialog = builder.create(); } class ImageAdapter extends BaseAdapter { private 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 null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView iv = new ImageView(context); iv.setImageResource(images[position]); iv.setAdjustViewBounds(true); iv.setLayoutParams(new Gallery.LayoutParams(150,150)); iv.setPadding(15, 10, 15, 10); return iv; } } class MyViewFactory implements ViewFactory { private Context context; public MyViewFactory(Context context) { this.context = context; } @Override public View makeView() { ImageView iv = new ImageView(context); iv.setAdjustViewBounds(true); iv.setLayoutParams(new ImageSwitcher.LayoutParams(160,160)); return iv; } } }