圖片浏覽gallery控件自定義風格,即加上灰色邊框:
1、main.xml文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:id="@+id/widget0"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Gallery
- android:id="@+id/Gallery"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- />
- </RelativeLayout>
2、attrs.xml文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="Gallery1">
- <attr name="android:galleryItemBackground" />
- </declare-styleable>
- </resources>
3、Activity代碼如下:
- public class gallery extends Activity {
- private int size;
- private List<Drawable> list = new ArrayList<Drawable>();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //全屏
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
-
- final Object data = getLastNonConfigurationInstance();
- if(data == null){
- getImage();
- }else{
- list = (List<Drawable>) data;
- }
-
- final Gallery g = (Gallery) findViewById(R.id.Gallery);
- g.setAdapter(new ImageAdapter(this,list));
- //默認顯示Gallery的中間一個圖片
- g.setSelection(size/2);
- }
-
- @Override
- public Object onRetainNonConfigurationInstance() {
- return list;
- }
-
- //獲取系統應用的圖標
- private List<Drawable> getImage(){
- PackageManager packageManager = this.getPackageManager();
- Intent intent = new Intent(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- List<ResolveInfo> infos = packageManager.queryIntentActivities(intent, 0);
- for(ResolveInfo info : infos){
- ActivityInfo ai = info.activityInfo;
- Drawable icon = ai.loadIcon(packageManager);
- list.add(icon);
- }
- return list;
- }
- }