当前位置:
代码迷
>>
Android
>> Android Gallery自定义作风
详细解决方案
Android Gallery自定义作风
热度:
98
发布时间:
2016-05-01 20:54:06.0
Android Gallery自定义风格
图片浏览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; }}
4、适配器代码如下:
public class ImageAdapter extends BaseAdapter { int mGalleryItemBackGround; private Context mContext; private List<Drawable> list; public ImageAdapter(Context mContext,List<Drawable> li) { this.mContext = mContext; this.list = li; TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery1); mGalleryItemBackGround = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); a.recycle(); } public int getCount() { return list.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { // 声明一个ImageView对象 ImageView i = new ImageView(mContext); BitmapFactory.Options options = new BitmapFactory.Options(); //设置图片的大小,宽高都为原来的二分之一,即图片为原来的四分之一 options.inSampleSize = 2; i.setImageDrawable(list.get(position)); //设置layout的宽高 i.setLayoutParams(new Gallery.LayoutParams(200,200));//layout //重新设置图片的宽高 i.setScaleType(ImageView.ScaleType.FIT_XY);//set scale type //设置图片的背景,即自定义的背景风格 i.setBackgroundResource(mGalleryItemBackGround); return i; } class ViewHolder{ ImageView image; }}
效果如下:
查看全文
相关解决方案
android 读取byte[]中的元素解决方案
android 标题栏兑现方式
android 中Activity向BroadcastReceiver发送数据,该怎么解决
Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
android:getSharedPreferences() 这是哪个类的方法解决思路
android 怎么判断一个程序是否联网
android 大量数据按周分组,该如何解决
android RadioButton如何设置默认选中
ksoap2-android-这个包,连接webService怎么设置超时
android 怎么重新设置锚点
android UI界面设计解决方案
android 图片对象获取的有关问题
android 怎么调用淘宝支付宝接口
Android 沿袭InputMethodService自定义输入法
android 关于服务连接的疑义
android 两个activity如何通信
android 怎么实现对view的放大和缩小
android 教程解决方法
android ID,该如何处理
准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
android UI线程与AsyncTask的有关问题
android(java)中的java.net能不能和c#的system.net.sockets进行tcp通信,该如何解决
android ListView 中的onItemClick Intent 没法跳转
android(java) 中文乱码的有关问题
c#c++,android,ios(iphone),php,java视屏课程 散分
android Post文件到ASP.NET的有关问题,能收到参数收不到文件
RIM 替 Android 开发者提供免费的 PlayBook!2月13日前
android 动态设立控件高度
Android test project 编译方法
android -相机使用教程(1)解决方法