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还有一个控件子类——WebView,WebView是一个浏览器控件,通过这个控件可以直接访问网页,如图7-16所示,打开一个网页。
图7-16 WebView
程序代码请参考代码清单7-18,完整代码请参考chapter7_4工程中chapter7_4_2代码部分。
【代码清单7-18】
通过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】
在AndroidManifest.xml中必须设置访问Internet互联网权限,否则会出现Web page not available错误。这是通过在文件AndroidManifest.xml中设置:<uses-permission android:name="android.permission.INTERNET"></uses-permission>而实现。
出自《Android开发案例驱动教程》第七章
图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开发案例驱动教程》第七章
相关文章推荐
- .Net程序员玩转Android开发---(9)绝对布局AbsoluteLayout
- Android基础入门教程——2.2.6 AbsoluteLayout(绝对布局)
- Android布局(5)--绝对布局(AbsoluteLayout)
- Android绝对布局管理器AbsoluteLayout
- Android布局之绝对布局AbsoluteLayout详解
- Android开发布局四_绝对布局(AbsoluteLayout)
- Android学习(十四)五布局之绝对布局AbsoluteLayout
- Android学习-五布局之绝对布局(AbsoluteLayout)
- Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局
- Android开发7:布局管理器4(网格布局GridLayout,绝对布局AbsoluteLayout)
- Android学习第四天之AbsoluteLayout绝对布局
- Android:控件布局(绝对布局)AbsoluteLayout
- Android五布局——绝对布局AbsoluteLayout
- Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局
- Android--五大布局(4)绝对布局AbsoluteLayout (实际中很少用到)
- android第三天--AbsoluteLayout绝对布局
- 【Android 开发教程】AbsoluteLayout绝对布局
- android第三天--AbsoluteLayout绝对布局
- Android AbsoluteLayout绝对布局
- Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局