7.4 AbsoluteLayout布局详解

简介: AbsoluteLayout绝对布局,指定了子元素准确的x/y坐标值,并显示在屏幕上。该布局没有屏幕边框,允许元素之间互相重叠。在实际中不提倡使用这种布局方式,因为它固定了位置,所以在进行屏幕旋转时有明显弊端。图7-15是绝对布局应用。<br>  <br><img src="http://dl.iteye.com/upload/attachment/520279/fdaf75d0-96c5-3
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: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开发案例驱动教程》第七章
目录
相关文章
|
7月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 垂直布局Vertical Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 垂直布局Vertical Layout
430 2
|
7月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout
399 2
Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )
Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )
708 0
Android布局——线性布局、相对布局、帧布局、表格布局、网格布局、约束布局
Android布局——线性布局、相对布局、帧布局、表格布局、网格布局、约束布局
273 1
Android布局——帧布局、表格布局、网格布局
Android布局——帧布局、表格布局、网格布局
TableLayout(表格布局)
前面我们已经学习了平时实际开发中用得较多的线性布局(LinearLayout)与相对布局(RelativeLayout),其实学完这两个基本就够用了,这一节我们会学习Android中的第三个布局:TableLayout(表格布局)!
115 0
Android使用绝对布局AbsoluteLayout动态添加控件
Android使用绝对布局AbsoluteLayout动态添加控件
192 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等