阅读116 返回首页    go 阿里云 go 技术社区[云栖]


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

  上一篇:go android.resource://这个Uri你知道吗
  下一篇:go Android进阶:ListView性能优化异步加载图片 使滑动效果流畅