您的位置:首页 > 移动开发 > Android开发

Android AbsoluteLayout布局(绝对布局)

2014-11-08 14:46 543 查看
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开发案例驱动教程》第七章






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: