DigitalClock 和 WebView 小例子

来源:互联网 发布:页游制作软件 编辑:程序博客网 时间:2024/03/29 10:01

除了一个 analog clock之外,你还可以显示一个数字时钟,使用DigitalClock view
添加 DigitalClock 元素到main.xml文件中,就像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    > 
 
    <AnalogClock android:id="@+id/clock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <DigitalClock android:id="@+id/clock2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />           
 
</LinearLayout>
Eclipse中按下F11。图7显示指针式时钟和数字时钟:


7 – 活动中的DigitalClock
WebView
WebView view允许你在你的应用程序中嵌入一个网页浏览器。添加一个新的文件到res/layout文件夹,把它命名为webview.xml ,用如下元素填充它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
    <WebView android:id="@+id/webview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "Hello, world!"
        />           
 
</LinearLayout>
添加一个新的classsrc/net.learn2develop.AndroidViews文件夹中,并且把它命名为 WebViewExample.java。这样填充它:
package net.learn2develop.AndroidViews;
 
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
 
public class WebViewExample extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
 
        WebView wv = (WebView) findViewById(R.id.webview1);       
        wv.loadUrl("http://www.mobiforge.com"); 
    }
}
WebView classloadUrl() method加载一个给出的URL的页面。修改AndroidManifest.xml 文件从而注册新的activity并且请求INTERNET 许可:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="net.learn2develop.AndroidViews"

      android:versionCode="1"

      android:versionName="1.0.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".ViewsActivity"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <activity android:name=".WebViewExample"
                  android:label="@string/app_name" />
 
    </application>

 

    <uses-permission android:name="android.permission.INTERNET">

    </uses-permission>

 

   <uses-sdk android:minSdkVersion="3" />

 

</manifest>

WebView view生效你需要添加INTERNET许可到AndroidManifest.xml文件中。像下面这样修改ViewsActivity.java文件,以开始WebViewExample activity
package net.learn2develop.AndroidViews;

 

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
 
public class ViewsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);     
        startActivity(new Intent(this, WebViewExample.class));
    }
}
按下 F11 Android模拟器上调试这个应用程序。图8显示活动的WebView view



8 – WebView显示一个网页
你还可以动态阐明一个HTML字符串,把它加载到一个WebView view中,使用 loadDataWithBaseURL() method
 
              WebView wv = (WebView) findViewById(R.id.webview1);       
 
        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String html = "<H1>A simple HTML page</H1><body>" +
            "<p>The quick brown fox jumps over the lazy dog</p>";
 
        wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
9 显示加载了HTML字符串的WebView


9 – 显示一个 HTML 字符串的内容
如果你有想要直接从你的project加载的静态页面,你可以在你的包的assets文件夹下面保存这个HTML页面。图10显示我已经在assets文件夹下添加了名为Index.html的页面:


10 – assets文件夹下添加一个HTML页面
Index.html 的内容如下所示:
<h1>A simple HTML page</h1>
<body>
   <p>The quick brown fox jumps over the lazy dog</p>
   <img src='http://www.google.com/logos/Logo_60wht.gif'/>
</body>
要使用保存在Index.html 文件中的内容加载那个WebView,这样使用loadUrl() method
           WebView wv = (WebView) findViewById(R.id.webview1);          
           wv.loadUrl("file:///android_asset/Index.html");
11 显示在WebView view中加载的内容:


11 – 使用WebView加载HTML 页面
总结
这这篇文章中,你看到了怎样在Android应用程序中实现内容和选择菜单。另外,你也了解了怎样使用其他的一些好用的view,例如AnalogClock以及DigitalClock。这就是这一系列的结尾了。我希望那个大家现在对于Android中的view是怎样工作的有了很好的掌握。祝大家在Android开发中获得成功。
 




原创粉丝点击