<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="110px"
android:layout_marginTop = "100px"
android:layout_alignParentLeft="true"
/>
<ImageSwitcher
android:id="@+id/image_switcher"
android:layout_width="90px"
android:layout_height="90px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/img_gallery" ></ImageSwitcher>
</RelativeLayout>

探索Gallery和ImageSwitcher布局-LMLPHP

当把上面Gallery一项属性改成android:layout_marginTop = "0px",出现下面的效果

探索Gallery和ImageSwitcher布局-LMLPHP

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="110px"
android:layout_marginTop = "0px"
android:layout_alignParentLeft="true"
/>
<ImageSwitcher
android:id="@+id/image_switcher"
android:layout_width="90px"
android:layout_height="90px" android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/img_gallery" ></ImageSwitcher>
</RelativeLayout>

删掉其中一行
探索Gallery和ImageSwitcher布局-LMLPHP

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

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="300px"
android:layout_marginTop = "0px"
android:layout_alignParentLeft="true"
/>
<ImageSwitcher
android:id="@+id/image_switcher"
android:layout_width="160px"
android:layout_height="160px"
android:layout_marginBottom = "70px"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/img_gallery" ></ImageSwitcher>
</RelativeLayout>

达到预期效果:

探索Gallery和ImageSwitcher布局-LMLPHP

完整控制代码:

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;
} } }
04-18 16:14
查看更多