2010-12-15 48 views
2

我正在尝试创建一个自定义图库视图。或者,也许我不明白我应该在这里做什么。我需要覆盖onFling()方法Gallery类,但我不明白我该如何做到这一点,因为我的主类必须从活动延伸。如何在android中制作自定义图库视图

我已经尝试做了一个名为CustomGallery延伸画廊类,但如果我尝试和运行应用程序,我得到一个强制关闭。

如何覆盖图库视图的onFling()方法

谢谢!

编辑

我试图但是,下面基督教的解决方案,这个类有遍布它的错误。很明显,我做错了。建议?

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.Gallery; 

public class CustomGallery extends Gallery 
{ 
    public CustomGallery(Context context) 
    { 
     super(context); 
    } 

    @Override 
    public Gallery(Context context) 
    { 
     this(context, null); 
    } 

    @Override 
    public Gallery(Context context, AttributeSet attrs) 
    { 
     this(context, attrs, R.attr.galleryStyle); 
    } 

    @Override 
    public Gallery(Context context, AttributeSet attrs, int defStyle) 
    { 
     // 
    } 
} 

EDIT 2

确定该懂了工作,THX基督徒!

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.Gallery; 

public class CustomGallery extends Gallery 
{ 
    public CustomGallery(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 

    } 

} 

回答

5

一个人如何改写为画廊查看onFling()方法?

  1. 创建一个类,扩展库(例如CustomGallery,XD)
  2. 覆盖的方法
  3. 使用这个类在你的布局。

只是用它就像如果你使用的是Gallery

<LinearLayout> 
... 
<com.your.package.CustomGallery 
    android:layout_width="fill_parent" 
    the rest of the things here/> 
... 
</LinearLayout> 

确保重载构造方法:

public class CustomGallery extends Gallery{ 

    public CustomGallery(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // this could be empty, but must be here. 
     // since it's a view to use from XML, 
     // you must override this constructor 
     // (not only the one that receives a context) 
    } 
} 
+0

该死的基督教你在键盘上睡觉笑?感谢您的快速回复!我正在尝试。只要我在这里,在XML中,我会调用画廊或CustomGallery(或任何我命名的类)? – Ribs 2010-12-15 23:58:12

+0

什么是“Pepe”? - > public Pepe(Context context,AttributeSet attrs) – Ribs 2010-12-16 00:01:18

+0

编辑:只要我在这里,在.java中,我会投到Gallery或CustomGallery(或任何我命名的类)? – Ribs 2010-12-16 00:05:05

相关问题