編輯:關於Android編程
這個網址有時候能打開,有時候打不開,看人品了。
昨天晚上寫安卓程序時,意外地發現:
Create Android Application 下面有一個Master/Detail Flow 選項,看著右邊的圖片引發我的好奇心。
然後就來剖析一下這個Master/Detail Flow。
下面的
activity_
res/values-large/refs.xml 和res/values-sw600dp/refs.xml 兩個附加的文件,純屬為了有助於理解這個程序如何幫助辨別是否使用雙面板模式。
Handling Different Android Devices and Displays 這一章有著更加詳細的勾勒。每個應用程序項目都擁有多套針對不同顯示器大小的資源。運行時,安卓系統自動地使用哪些最為匹配物理設備大小的資源集。當然,values-large 和values-sw600dp用於大顯示器的設備。在這些文件夾中ref.xml文件僅僅是聲明一個導致所雙窗口布局可以被使用的別名:
- @layout/activity_item_twopane
雖然 Master/Detail Flow模板的結構在一開始顯示的很混亂,但是隨著默認模板在本章的隨後部分被修改,它的概念將會變得更加清晰。隨之出現的是,其中很大一部分由模板提供的功能,在許多Master/Detail實現中是不需要修改的。
在本章的剩余部分,該MasterDetailFlow項目將被修改,以使主列表顯示網站名稱的列表,並改變以包含web視圖對象,而不是目前的TextView的詳細信息窗格中。當一個網站是由用戶選擇,相應的網頁將隨後加載和在細節窗格中顯示
In the rest of this chapter, the MasterDetailFlow project will be modified such that the master list displays a list of web site names and the detail pane altered to contain a WebView object instead of the current TextView. When a web site is selected by the user, the corresponding web page will subsequently load and display in the detail pane.
在隨後的章節中,MasterDetailFlow project將會被修改,以至於Master列表顯示的是一列網址名稱,Detail面板變為包含一個WebView對象,而不是當前的TextView。當用戶選擇了一個網址,相應的網頁將被隨後加載並顯示在Detail pane中
The content for the example as it currently stands is defined by the DummyContent class file。因此,開始通過選擇DummyContent.java文件,並檢查代碼。在文件的底部是一個聲明了一個名為DummyItem類,這是目前能夠存儲兩個String對象,分別表示content string和一個ID 。另一方面,更新後的項目,將需要每個item object包含一個ID string,用於網站名稱的字符串,和一個網址URL。要添加這些功能,修改DummyItem類,以便它的內容如下:
public static class DummyItem { public String id; public String website_name; public String website_url; public DummyItem(String id, String website_name, String website_url) { this.id = id; this.website_name = website_name; this.website_url = website_url; } @Override public String toString() { return website_name; } }
注意到這個類現在添加了3個Items。分別顯示為 “Item 1”, “Item 2” and “Item 3”:
public static MapITEM_MAP = new HashMap (); static { // Add 3 sample items. addItem(new DummyItem(1, Item 1)); addItem(new DummyItem(2, Item 2)); addItem(new DummyItem(3, Item 3)); }
his code needs to be modified to initialize the data model with the required web site data:
public static MapITEM_MAP = new HashMap (); static { // Add 3 sample items. addItem(new DummyItem(1, eBookFrenzy, http://www.ebookfrenzy.com)); addItem(new DummyItem(2, Google, http://www.google.com)); addItem(new DummyItem(3, Android, http://www.android.com)); }
The code now takes advantage of the modified DummyItem class to store an ID, web site name and URL for each item.
上面幾句話只可意會,意思是把
addItem(new DummyItem(1, Item 1));修改為
addItem(new DummyItem(1, eBookFrenzy, http://www.ebookfrenzy.com));
修改Detail Pane:
The detail information shown to the user when an item is selected from the master list is currently displayed via the layout contained in the fragment_website_detail.xml file. By default this contains a single view in form of a TextView. Since the TextView class is not capable of displaying a web page, this needs to be changed to a WebView object for the purposes of this tutorial. To achieve this, navigate to the res -> layout -> fragment_website_detail.xml file in the Project Explorer panel and double click on it to load it. Switch to the Graphical Layout tool display if it is not already displayed before right-clicking on the white background of the display canvas (which represents the TextView). From the resulting menu, select the Change Widget Type… menu option. In the Change Widget Type dialog, change the widget type from TextView to WebView before clicking on OK to commit the change.(下面是意譯,因為直譯對我來說,實在是太痛苦了,不過我還是能看懂英文的)
雙擊打開res -> layout ->fragment_
At this point the user interface detail panel has been modified but the corresponding Java class is still designed for working with a TextView object instead of a WebView. Load the source code for this class by double clicking on the WebsiteDetailFragment.java file in the Project Explorer panel. Within the source file locate the onCreateView() method which should read as outlined in the following listing:(以下為意譯)
雖然res -> layout ->fragment_
把((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);修改為:((WebView) rootView.findViewById(R.id.item_detail)).loadUrl(mItem.website_url);即可。
package com.example.masterdetailflow; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; . . . @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_website_detail, container, false); // Show the dummy content as text in a TextView. if (mItem != null) { ((TextView) rootView.findViewById(R.id.website_detail)) .setText(mItem.content); } return rootView; }
In order to load the web page URL corresponding to the currently selected item only one line of code needs to be changed. Once this change has been made, the method should read as follows (note also the addition of the import directive for the android.webkit.WebView library):
package com.example.masterdetailflow; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; . . . @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_website_detail, container, false); // Show the dummy content as text in a TextView. if (mItem != null) { ((WebView) rootView.findViewById(R.id.website_detail)) .loadUrl(mItem.website_url); return rootView; }
All that this change does is find the view with the ID of website_detail (this was formally the TextView but is now a WebView), extracts the URL of the web site from the selected item and instructs the WebView object to load that page.
最後一步是通過manifest文件添加網絡權限到應用程序中。這將會允許WebView 對象訪問網絡下載網絡頁面。修改項目中的 AndroidManifest.xml文件 。 添加合適的權限行到該文件中:
. . .
Compile and run the application on a suitably configured emulator or an attached Android device. Depending on the size of the display, the application will appear either in small screen or two-pane mode. Regardless, the master list should appear primed with the names of the three web sites defined in the content model. Selecting an item should cause the corresponding web site to appear in the detail panel as illustrated in two-pane mode in Figure 23-5:
總結:
一個master/detail用戶界面由項目的Master列表,選中它,一個細節面板中會顯示有關該選擇的更多信息。master/detail Flow 是Android ADT bundle提供的一個模板,允許一個master/detail管理被快速和相對容易創建。在本章中的演示中,對默認模板文件稍加修改,各種基於Master/Detail的功能可以通過少量的修改和設計付出即可實現。
SQLite是一款輕量級的關系型數據庫,它運算速度快,占用資源少,通常只需要幾百k的內存就夠了,支持標准的sql語法和數據庫的ACID事務。在android中為了能夠更加
AlertDialog生成的對話框可分為4個區域:圖標區,標題區,內容區,按鈕區結構如圖:AlertDialog對話框的使用:1,創建AlertDialog.Builde
最近沒事做就寫了一下PopupWindow,希望對有些人有點幫助。照常先看一下完成後的結果(界面比較難看就不要吐槽了)點擊地理位置然後彈出的PopupWindow,數據我
理解android視圖 對於android設備我們所看到的區域其實和它在底層的繪制有著很大的關系,很多時候我們都只關心我們所看到的,那麼在底層一點它到底是怎麼樣的一個東西