首页 新闻 会员 周边 捐助

Android GridView无法显示自定义View

0
悬赏园豆:20 [已解决问题] 解决于 2014-08-18 16:23

想用GridView来显示自己写的自定义View(可能会实现一些动画之类的东西),GridView设置的是Adapter是自定义的,在getView方法返回自定义的View,但是却显示不出来。

已经试验过的:

1,如果getView方法返回的是ImageView就可以正常显示出来;

2,自定义View直接放在Activity的setContentView中可以显示,放到Adapter的getView方法返回则显示不出来。

不知道我的自定义View和ImageView关键点的区别在哪儿,有朋友遇到过类似的问题吗?希望能给一点提示。

 

--------

以下是代码

1,这是Adapter的代码:

class WarriorAdapter extends BaseAdapter{
        Context mCtx;
        
        public WarriorAdapter(Context ctx){
            mCtx = ctx;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return 20;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            return null;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            ImageView view = null;
//            if(convertView != null && ImageView.class.isInstance(convertView)){
//                view = (ImageView) convertView;
//            }else{
//                view = new ImageView(mCtx);
//                view.setImageResource(R.drawable.bg);
//            }
            
            ChrView view = null;
            if(convertView != null && ChrView.class.isInstance(convertView)){
                view = (ChrView) convertView;
            }else{
                view = new ChrView(mCtx, 
                        BitmapFactory.decodeResource(getResources(), R.drawable.bg), 
                        BitmapFactory.decodeResource(getResources(), R.drawable.stars_bg), 
                        BitmapFactory.decodeResource(getResources(), R.drawable.grade0));
            }
                
            return view;
        }
        
    }

 

2,这个是我自定义View的代码:

class ChrView extends View{
    
    Bitmap bmBg;
    Bitmap bmDownBar;
    Bitmap bmBird;
    
    int bgX;
    int bgY;
    int birdX;
    int birdY;
    int downBarX;
    int downBarY;
    int headX;
    int headY;
    int width;
    int height;
    
    
    /**
     * 
     * @param context
     * @param cfg
     * @param warrBg 
     * @param bownBar
     * @param bird
     */
    public ChrView(Context context, Bitmap warrBg, Bitmap downBar, Bitmap bird) {
        super(context);
        
        bmBg = warrBg;
        bmDownBar = downBar;
        bmBird = bird;
        
        //Set positions & sizes
        bgX = 17;
        bgY = 0;
        birdX = 0; //-17);
        birdY = 62;
        downBarX = 9; //-8);
        downBarY = 98;
        headX = 37; //20);
        headY = 34;
        
        width = downBarX + bmDownBar.getWidth();
        height = downBarY + bmDownBar.getHeight();
    }
    
    
    /* (non-Javadoc)
     * @see android.view.View#onAttachedToWindow()
     */
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        try{
//            //Set positions & sizes
//            bgX = 17;
//            bgY = 0;
//            birdX = 0; //-17);
//            birdY = 62;
//            downBarX = 9; //-8);
//            downBarY = 98;
//            headX = 37; //20);
//            headY = 34;
//            
//            width = downBarX + bmDownBar.getWidth();
//            height = downBarY + bmDownBar.getHeight();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
    /* (non-Javadoc)
     * @see android.view.View#onDetachedFromWindow()
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        try{
            
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
    
    /* (non-Javadoc)
     * @see android.view.View#getMinimumHeight()
     */
    @Override
    public int getMinimumHeight() {
        //return super.getMinimumHeight();
        return height;
    }
    
    
    /* (non-Javadoc)
     * @see android.view.View#getMinimumWidth()
     */
    @Override
    public int getMinimumWidth() {
        //return super.getMinimumWidth();
        return width;
    }

    
    
    /* (non-Javadoc)
     * @see android.view.View#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        try{
            canvas.drawBitmap(bmBg, bgX, bgY, null);
            canvas.drawBitmap(bmBird, birdX, birdY, null);
            canvas.drawBitmap(bmDownBar, downBarX, downBarY, null);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
Charlie123的主页 Charlie123 | 初学一级 | 园豆:142
提问于:2014-08-18 15:45
< >
分享
最佳答案
0

忘记了给设置最小宽高。

光重写了getMinimumHeight(), getMinimumWidth(),却没有给他进行设置。所以可能会默认它的宽高为0,于是就不画了。

在onAttachedToWindow()方法中加入两句:setMinimunWidth()和setMinimunHeight()。

Charlie123 | 初学一级 |园豆:142 | 2014-08-18 16:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册