android - coverflow 未显示在 Android 的 xml 布局上

标签 android xml layout coverflow

我正在使用网站上的一些代码作为封面流程,

http://www.inter-fuser.com/2010/01/android-coverflow-widget.html

normal working coverflow

这个封面流程效果很好,没有任何问题。但是,当我尝试将其放入 xml 布局中时,它不会显示,仅显示 coverflow 的黑色背景壁纸,但 coverfow 丢失。

missing coverflow in layout

我想将它放在 xml 布局中的原因是因为需要将按钮放在页面底部。所以我决定在屏幕下方的封面流下方制作一个带有按钮的 xml 布局。

有什么问题或者我遗漏了什么导致 coverflow 在 xml 布局中无法工作?

我的代码中可能缺少一些东西才能使其工作。我做了以下事情来尝试让它发挥作用。代码作者使它看起来像您所要做的就是使用包含的示例代码来设置内容 View 。并在布局中创建一个简单的 xml 文件,它应该可以工作。

我不明白为什么封面流本身工作得这么好,但放在布局内部时却不起作用。

是否有一个检查 list ,列出了此类工作需要完成的事情?

coverflow 代码的作者添加了两行注释掉的行作为示例,您可以在创建自己的自定义 xml View 时使用该示例

         //Use this if you want to use XML layout file
    //setContentView(R.layout.main);
    //coverFlow =  (CoverFlow) findViewById(R.id.coverflow);

我删除了这些行的注释,以便我可以使用它们。

接下来我将以下代码添加到 xml 布局

      <com.vagina.destruction.CoverFlow
        android:id="@+id/coverflow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
      </com.vagina.destruction.CoverFlow>

这是我从 eclipse 中的可视布局编辑器收到的错误消息

以下类无法实例化: - com.vagina.destructon.CoverFlow(公开课,显示错误日志) - com.vagina.destruction.CoverFlowExample(公开课,显示错误日志) 有关更多详细信息,请参阅错误日志(窗口 > 显示 View )。 提示:在自定义 View 中使用 View.isInEditMode() 可以在 Eclipse 中显示时跳过代码

这是 coverflow 示例 Activity 开始处的代码部分

 public class CoverFlowExample extends Activity implements OnItemClickListener {
 /** Called when the activity is first created. */

int imageCount = 0;
Cursor cur;
String toastResult;
String pathName;
 ArrayList<String> list1 = new ArrayList<String>();
private int blocker = 0;
private int imagePosition;
private int pathIndex;
private int indexer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CoverFlow coverFlow;
    coverFlow = new CoverFlow(this);

    Bundle extras = getIntent().getExtras();
    if(extras !=null) {

     imagePosition = extras.getInt("imagePosition");

    }


    String[] proj2 = {MediaStore.Images.Media.DATA};
    Cursor cur2 = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj2,  MediaStore.Images.Media.IS_PRIVATE + "='" + 1 +"'", null, null);

    imageCount = cur2.getCount();


    cur2.close();

    coverFlow.setAdapter(new ImageAdapter(this));

    ImageAdapter coverImageAdapter =  new ImageAdapter(this);

    coverFlow.setAdapter(coverImageAdapter);

    coverFlow.setSpacing(-10);
    coverFlow.setSelection(imageCount, true);

 //  setContentView(coverFlow);

    coverFlow.setOnItemClickListener(this);

    coverFlow.setSelection(imagePosition);


    coverFlow =  (CoverFlow) findViewById(R.id.coverflow);

    //Use this if you want to use XML layout file
         setContentView(R.layout.activity_coverflow);
    coverFlow =  (CoverFlow) findViewById(R.id.coverflow);

    // centers the image on coverflow to the same image selected in the previous activity
      coverFlow.setBackgroundResource(R.drawable.blanklarge);

最佳答案

使用这个

     public class CoverFlowExample extends Activity implements OnItemClickListener {
     /** Called when the activity is first created. */

    int imageCount = 0;
    Cursor cur;
    String toastResult;
    String pathName;
     ArrayList<String> list1 = new ArrayList<String>();
    private int blocker = 0;
    private int imagePosition;
    private int pathIndex;
    private int indexer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Use this if you want to use XML layout file
        setContentView(R.layout.activity_coverflow);
        CoverFlow coverFlow;
        coverFlow =  (CoverFlow) findViewById(R.id.coverflow);

/*        CoverFlow coverFlow;
        coverFlow = new CoverFlow(this); */

        Bundle extras = getIntent().getExtras();
        if(extras !=null) {

         imagePosition = extras.getInt("imagePosition");

        }


        String[] proj2 = {MediaStore.Images.Media.DATA};
        Cursor cur2 = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj2,  MediaStore.Images.Media.IS_PRIVATE + "='" + 1 +"'", null, null);

        imageCount = cur2.getCount();


        cur2.close();

        coverFlow.setAdapter(new ImageAdapter(this));

        ImageAdapter coverImageAdapter =  new ImageAdapter(this);

        coverFlow.setAdapter(coverImageAdapter);

        coverFlow.setSpacing(-10);
        coverFlow.setSelection(imageCount, true);

        coverFlow.setOnItemClickListener(this);

        coverFlow.setSelection(imagePosition);



        // centers the image on coverflow to the same image selected in the previous activity
          coverFlow.setBackgroundResource(R.drawable.blanklarge);

基本上,问题在于,一旦您在 xml 中声明了内容,一旦您使用 setCOntentView 膨胀它们,就会自动创建 opbjects。您可以使用 findViewById 获取那些已创建的对象,而无需使用“x = new x()”再次创建它们

关于android - coverflow 未显示在 Android 的 xml 布局上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13695927/

相关文章:

mysql - 使用 XPath 和编码导入 xml

node.js - 表示没有找到/使用布局

java - 如何使用collapsing toolbar布局实现这种布局效果

java - 私有(private)成员变量变为 null

html - 如何将 svg + xml 放入 css 样式文档而不将其编码为 base64?

xml - 使用 VBA 另存为 XML?

html - 使两个div并排出现

android - 设计 Android 应用程序 - 最佳实践?

android - 如何在另一个类中委托(delegate)一个字符串而不重新定义它?

android - 使用 jar 的推送通知不起作用