AbsoluteLayout绝对布局,指定了子元素准确的x/y坐标值,并显示在屏幕上。该布局没有屏幕边框,允许元素之间互相重叠。在实际中不提倡使用这种布局方式,因为它固定了位置,所以在进行屏幕旋转时有明显弊端。图7-15是绝对布局应用。
 

7.4 AbsoluteLayout布局详解 _职场


图7-15 AbsoluteLayout
AbsoluteLayout布局文件请参考代码清单7-17,完整代码请参考chapter7_4工程中absolutelayout.xml代码部分(chapter7_4/res/layout/absolutelayout.xml)。
【代码清单7-17】

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <AbsoluteLayout android:layout_width="fill_parent" 
  3. android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
  4. <TextView android:layout_width="wrap_content" 
  5. android:layout_height="wrap_content" android:text="@string/beijing" 
  6. android:layout_x="10px" android:layout_y="10px"> 
  7. </TextView> 
  8. <TextView android:layout_width="wrap_content" 
  9. android:layout_height="wrap_content" android:text="@string/shanghai" 
  10. android:layout_x="80px" android:layout_y="80px"> 
  11. </TextView> 
  12. <TextView android:layout_width="wrap_content" 
  13. android:layout_height="wrap_content" android:text="@string/tianjin" 
  14. android:layout_x="150px" android:layout_y="150px"> 
  15. </TextView> 
  16. </AbsoluteLayout>  


AbsoluteLayout还有一个控件子类——WebView,WebView是一个浏览器控件,通过这个控件可以直接访问网页,如图7-16所示,打开一个网页。
 
 

7.4 AbsoluteLayout布局详解 _职场_02



图7-16 WebView
程序代码请参考代码清单7-18,完整代码请参考chapter7_4工程中chapter7_4_2代码部分。
【代码清单7-18】

  1. public class chapter7_4_2 extends Activity { 
  2. WebView browser; 
  3.  
  4. @Override 
  5. public void onCreate(Bundle savedInstanceState) { 
  6. super.onCreate(savedInstanceState); 
  7. requestWindowFeature(Window.FEATURE_NO_TITLE); 
  8. setContentView(R.layout.webviewlayout); 
  9.  
  10. browser = (WebView) findViewById(R.id.webkit); 
  11. browser.loadUrl("http://www.51work6.com/index.html"); 
  12.  
  13. }  


通过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】

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent"> 
  5. <WebView android:id="@+id/webkit" android:layout_width="wrap_content" 
  6. android:layout_height="wrap_content"></WebView> 
  7. </LinearLayout>  


 在AndroidManifest.xml中必须设置访问Internet互联网权限,否则会出现Web page not available错误。这是通过在文件AndroidManifest.xml中设置:<uses-permission android:name="android.permission.INTERNET"></uses-permission>而实 现。
                               出自《Android开发案例驱动教程》第七章