Android AbsoluteLayout布局(绝对布局)

AbsoluteLayout,顾名思义,就是绝对位置的布局;也可以叫做坐标布局,也就是指定元素的绝对位置(或者叫绝对坐标值),指定了子元素准确的x/y坐标值,并显示在屏幕上。该布局没有屏幕边框,允许元素之间互相重叠,。这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。因为它固定了位置,所以在进行屏幕旋转时有明显弊端。

  
 

图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:android="http://schemas.android.com/apk/res/android">  
    <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("http://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: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/webkit" 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开发案例驱动教程》第七章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值