標籤:android style blog http io ar color os sp
畫廊 滑動顯示多個圖片
layout
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 8 9 <Gallery 10 android:id="@+id/gallery" 11 android:spacing="5px" 12 android:unselectedAlpha="0.5" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" />15 16 17 </LinearLayout>
Activity
1 package tea_xqx; 2 3 import com.example.tea.R; 4 5 import android.app.Activity; 6 import android.content.res.TypedArray; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.ViewGroup;10 import android.widget.AdapterView;11 import android.widget.AdapterView.OnItemClickListener;12 import android.widget.BaseAdapter;13 import android.widget.Gallery;14 import android.widget.ImageView;15 import android.widget.TextView;16 import android.widget.Toast;17 18 public class MenuTeaTools extends Activity{19 //設定畫廊圖片20 private int[] imageId = new int[] { R.drawable.menu_tea_lvcha,R.drawable.menu_tea_hongcha,R.drawable.menu_tea_baicha,R.drawable.menu_tea_heicha,R.drawable.menu_tea_huangcha,R.drawable.menu_tea_wulongcha}; 21 @Override22 protected void onCreate(Bundle savedInstanceState) {23 // TODO Auto-generated method stub24 super.onCreate(savedInstanceState);25 setContentView(R.layout.menu_tools);26 27 Gallery gallery = (Gallery) findViewById(R.id.gallery);28 BaseAdapter adapter = new BaseAdapter() { 29 @Override 30 public View getView(int position, View convertView, ViewGroup parent) { 31 ImageView imageview; 32 if (convertView == null) { 33 imageview = new ImageView(MenuTeaTools.this); 34 imageview.setScaleType(ImageView.ScaleType.FIT_XY); 35 imageview.setLayoutParams(new Gallery.LayoutParams(500, 400)); 36 TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery); 37 imageview.setBackgroundResource(typedArray.getResourceId( 38 R.styleable.Gallery_android_galleryItemBackground, 39 0)); 40 imageview.setPadding(5, 0, 5, 0); 41 } 42 else 43 { 44 imageview = (ImageView) convertView; 45 } 46 imageview.setImageResource(imageId[position]); 47 return imageview; 48 } 49 50 @Override 51 public long getItemId(int position) { 52 return position; 53 } 54 55 @Override 56 public Object getItem(int position) { 57 return position; 58 } 59 60 @Override 61 public int getCount() { 62 return imageId.length; 63 } 64 }; 65 gallery.setAdapter(adapter); 66 gallery.setSelection(imageId.length / 2); //預設顯示的圖片的id 67 //畫廊圖片的點擊事件68 gallery.setOnItemClickListener(new OnItemClickListener() { 69 @Override 70 public void onItemClick(AdapterView<?> parent, View view, 71 int position, long id) {72 73 Toast.makeText(MenuTeaTools.this, 74 "第" + String.valueOf(position+1) + "張圖片被選中", 75 Toast.LENGTH_SHORT).show(); 76 } 77 }); 78 } 79 80 81 82 }
最後在res/values/string.xml中添加一段代碼
1 <declare-styleable name="Gallery"> 2 <attr name="android:galleryItemBackground" /> 3 </declare-styleable>
這樣便完成了一個畫廊的效果
安卓開發實現畫廊效果