【问题标题】:How to add spinner in splash screen in android phonegap app如何在 android phonegap 应用程序的启动画面中添加微调器
【发布时间】:2013-06-11 00:36:01
【问题描述】:

我正在使用 Phonegap + Jquery mobile 开发一个 android 应用程序,我在启动应用程序时显示了一个初始屏幕,但它是一个静态 png 文件,现在我的客户不断要求一些加载指示器,如微调器或进度条用户等待应用加载时的初始屏幕。

我按照一个示例在初始屏幕中显示进度条,但它不起作用,下面是我的代码。

主java:

super.onCreate(savedInstanceState);

final Activity activity = this;
final ProgressBar progessBar1;
View footer = View.inflate(getContext(), R.layout.spinner, null);
root.addView(footer);

progessBar1 = (ProgressBar) findViewById(R.id.progressBar1);

this.appView.setWebChromeClient(new WebChromeClient() {

        public void onProgressChanged(WebView view, int progress) { 
            activity.setProgress(progress * 1000);
            if(progress < 100 && progessBar1.getVisibility() == ProgressBar.GONE) {
                progessBar1.setVisibility(ProgressBar.VISIBLE);
            }
            progessBar1.setProgress(progress);
            if(progress == 100) {
                progessBar1.setVisibility(ProgressBar.GONE);
            }

            Log.d("Progress", progress+"");

         }
    });

super.setIntegerProperty("splashscreen", R.drawable.splash);
super.setStringProperty("loadingDialog", "Wait, loading packages ...");


super.loadUrl("file:///android_asset/www/index.html", 12000);
super.setIntegerProperty("loadUrlTimeoutValue", 12000); 

以及 res/layout 中的 spinner.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:maxHeight="10dip"
        android:minHeight="10dip" />

</LinearLayout>

但它不显示进度条。

我的问题:

  1. 谁能帮忙指出我哪里做错了,谢谢。

  2. 任何其他方式来做到这一点,实际上我只是想在初始屏幕上显示一个微调器,就可以了。

【问题讨论】:

    标签: android cordova screen spinner splash-screen


    【解决方案1】:

    确保您的初始图像具有透明背景。

    Splashscreen 占据了整个屏幕空间,将进度条隐藏在后面。我尝试将进度条带入到启动屏幕上,该进度条也没有显示在初始屏幕上。

    我尝试运行您的代码,它会引发空指针异常

        this.appView.setWebChromeClient.....
    

    因为 appView 在这一行为空。

    我将代码更改为以下内容((即在 setwebchromeclient 之前加载 url),它会同时显示初始屏幕和进度条。

        super.onCreate(savedInstanceState);
    
        final Activity activity = this;
        final ProgressBar progessBar1;
    
        super.setIntegerProperty("splashscreen", R.drawable.splash);
        super.setStringProperty("loadingDialog", "Wait, loading packages ...");
        super.loadUrl("file:///android_asset/www/index.html", 20000);
        super.setIntegerProperty("loadUrlTimeoutValue", 20000);
    
    
        View footer = View.inflate(getContext(), R.layout.testspinner, null);
        root.addView(footer);
    
        progessBar1 = (ProgressBar) findViewById(R.id.progressBar11);
    
        this.appView.setWebChromeClient(new WebChromeClient() {
    
            public void onProgressChanged(WebView view, int progress) {
                activity.setProgress(progress * 1000);
                if (progress < 100
                        && progessBar1.getVisibility() == ProgressBar.GONE) {
                    progessBar1.setVisibility(ProgressBar.VISIBLE);
                }
                progessBar1.setProgress(progress);
                if (progress == 100) {
                    progessBar1.setVisibility(ProgressBar.GONE);
                }
    
                Log.d("Progress", progress + "");
    
            }
        });
    

    【讨论】:

    • 嗨 Catimos,感谢您的回复,我仍然无法正常工作,可以分享您的代码示例吗?
    • 你的代码没问题。无需更改。 splash.png(或 splash.9.png)应该具有透明背景。我使用paint.net 来编辑图像。
    • 进度条在闪屏后面运行。如果您检查 logcat,则会显示进度值。
    • 感谢回复,我尝试将背景更改为透明,但还是看不到,只是背景变黑了。
    • 我尝试运行您的代码并发现问题。我相应地更新了我的答案。
    【解决方案2】:

    我相信这也不会是你想要的样子。在某一点添加了相同的 ProgressBar 小部件,并且它是相同的(丑陋的)加载栏 - 至少在我的 droid3 上 - 当您通过浏览器查看网页时,它会显示在操作系统浏览器上。这不利于“原生”的外观和感觉。

    【讨论】:

    • 你好obitusis,谢谢你的回复,我也觉得,你有没有更好的解决方案而不是显示进度条?
    • 一个 html/css/javascript 解决方案可能是这里最简单的替代方案
    【解决方案3】:

    是的,您收到空指针异常,因为代码中进度条的 id 与布局文件中的 id 不匹配。

    下面一行

    com.accoladewios.apps.jetabout.MainActivity$1.onProgressChanged(MainActivity.java:42)
    

    表示空指针异常的第42行。

    splash.xml 中的进度条 ID 是 progressBar1。然而 您的代码中的以下行使用 progressbar1

    progessBar1 = (ProgressBar) findViewById(R.id.progressbar1 );
    

    如果您调试代码,则在运行上述行后变量 progessBar1 将为空。

    修复此问题,启动画面应显示进度条。

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      相关资源
      最近更新 更多