Gallery:

A view that shows items in a center-locked, horizontally scrolling list.

The default values for the Gallery assume you will be usingTheme_galleryItemBackgroundas the background for each View given to the Gallery from the Adapter. If you are not doing this, you may need to adjust some Gallery properties, such as the spacing.

Views given to the Gallery should useGallery.LayoutParamsas their layout parameters type.

即,画廊,可以左右滑动,选中的项默认为居中。

重要属性:

android:animationDuration Sets how long a transition animation should run (in milliseconds) when layout has changed.

android:spacing 设置每个子项之间的距离

android:unselectedAlpha Sets the alpha on the items that are not selected.

示例1:

<?xml version="1.0" encoding="utf-8"?> <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" />

public class Gallery1 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallery_1); // Reference the Gallery view Gallery g = (Gallery) findViewById(R.id.gallery); // Set the adapter to our custom adapter (below) g.setAdapter(new ImageAdapter(this)); // Set a item click listener, and just Toast the clicked position g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show(); } }); // We also want to show context menu for longpressed items in the gallery registerForContextMenu(g); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(R.string.gallery_2_text); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show(); return true; } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; public ImageAdapter(Context c) { mContext = c; // See res/values/attrs.xml for the <declare-styleable> that defines // Gallery1. TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); mGalleryItemBackground = a.getResourceId( R.styleable.Gallery1_android_galleryItemBackground, 0); a.recycle(); } public int getCount() { return mImageIds.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 i = new ImageView(mContext); i.setImageResource(mImageIds[position]); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setLayoutParams(new Gallery.LayoutParams(136, 88)); // The preferred Gallery item background i.setBackgroundResource(mGalleryItemBackground); return i; } private Context mContext; private Integer[] mImageIds = { R.drawable.gallery_photo_1, R.drawable.gallery_photo_2, R.drawable.gallery_photo_3, R.drawable.gallery_photo_4, R.drawable.gallery_photo_5, R.drawable.gallery_photo_6, R.drawable.gallery_photo_7, R.drawable.gallery_photo_8 }; } }

示例2:

public class Gallery2 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallery_2); // Get a cursor with all people Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null); startManagingCursor(c); SpinnerAdapter adapter = new SimpleCursorAdapter(this, // Use a template that displays a text view android.R.layout.simple_gallery_item, // Give the cursor to the list adatper c, // Map the NAME column in the people database to... new String[] {People.NAME}, // The "text1" view defined in the XML template new int[] { android.R.id.text1 }); Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(adapter); } }

更多相关文章

  1. Android ApiDemos示例解析(139):Views->Layouts->Baseline->Nest
  2. Android中话bitmap的简单示例
  3. Android ApiDemos示例解析(90):OS->Sensors
  4. Android ApiDemos示例解析(83):Graphics->Typefaces
  5. Android viewpager 实现画廊效果 左右可见部分子view
  6. 在 Eclipse 中导入 Android 示例程序
  7. android map api v2 示例 步骤及问题
  8. Android 通知的基本用法示例代码
  9. android Gallery(画廊)以及BaseAdapter

随机推荐

  1. android使用http协议上传文件
  2. Android(安卓)log日志记录方法
  3. Android(安卓)Matrix
  4. Android如何下抓取tcp数据包
  5. Android(安卓)4编程入门经典
  6. Android获取当前位置经纬度(非第三方地图
  7. Android分享文稿 ( by quqi99 )
  8. android中ContactsContract获取联系人的
  9. Android学习笔记(二)开发环境的安装
  10. Android之网络请求11————Retrofit的