7.4 AbsoluteLayout布局詳解
AbsoluteLayout絕對布局,指定了子元素準確的x/y坐標值,並顯示在屏幕上。該布局沒有屏幕邊框,允許元素之間互相重疊。在實際中不提倡使用這種布局方式,因為它固定了位置,所以在進行屏幕旋轉時有明顯弊端。圖7-15是絕對布局應用。
圖7-15 AbsoluteLayout
AbsoluteLayout布局文件請參考代碼清單7-17,完整代碼請參考chapter7_4工程中absolutelayout.xml代碼部分(chapter7_4/res/layout/absolutelayout.xml)。
【代碼清單7-17】
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:andro>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/beijing"
android:layout_x="10px" android:layout_y="10px">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/shanghai"
android:layout_x="80px" android:layout_y="80px">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/tianjin"
android:layout_x="150px" android:layout_y="150px">
</TextView>
</AbsoluteLayout>
AbsoluteLayout還有一個控件子類——WebView,WebView是一個瀏覽器控件,通過這個控件可以直接訪問網頁,如圖7-16所示,打開一個網頁。

圖7-16 WebView
程序代碼請參考代碼清單7-18,完整代碼請參考chapter7_4工程中chapter7_4_2代碼部分。
【代碼清單7-18】
public class chapter7_4_2 extends Activity {
WebView browser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webviewlayout);
browser = (WebView) findViewById(R.id.webkit);
browser.loadUrl("https://www.51work6.com/index.html");
}
}
通過findViewById()方法找到布局文件main.xml中的叫“webkit”的WebView控件。使用loadUrl()方法加載網頁。還可以通過getSettings().setJavaScriptEnabled(true)設置開啟javascript,否則WebView不執行javascript腳本。
布局文件請參考代碼清單7-19,完整代碼請參考chapter7_4工程中webviewlayout.xml代碼部分(chapter7_4/res/layout/webviewlayout.xml)。
【代碼清單7-19】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView android: android:layout_width="wrap_content"
android:layout_height="wrap_content"></WebView>
</LinearLayout>
在AndroidManifest.xml中必須設置訪問Internet互聯網權限,否則會出現Web page not available錯誤。這是通過在文件AndroidManifest.xml中設置:<uses-permission android:name="android.permission.INTERNET"></uses-permission>而實現。
出自《Android開發案例驅動教程》第七章
最後更新:2017-04-02 06:51:45
上一篇:
android.resource://這個Uri你知道嗎
下一篇:
Android進階:ListView性能優化異步加載圖片 使滑動效果流暢
丟掉DDoS的八個幻想
Spring Cloud Bus書目錄
數字貨幣是貨幣?資產?還是別的?場外交易係統開發
表格存儲在QCon2017的分享
雲棲大會;“互聯網+”博覽會;智慧棗莊起航
OssImport係列之二——單機部署
Windws OS 一些概念
與微服務一脈相承,Serverless適用何種場景?會帶來哪些衝擊?
Breakthrough in Alibaba Cloud Computing Capabilities - BigBench Reaches 100 TB World Record
讀書筆記:Apache Hadoop YARN: Yet Anothe Resource Negotiator