Android入门学习五:用户界面

[size=large][b]屏幕的构成[/b][/size]

活动包含了视图和试图组。视图是一个可以在屏幕上显示的小部件,例如按钮、标签、文件框。视图派生自基类android.view.View

一个或多个视图可以组成一个视图组,视图组(本身就是一种特殊的视图类型)提供了一种布局,您可以按该布局指定视图的外观和顺序,视图组派生于基类 android.view.viewGroup。android支持以下五种视图组:

[size=large][b]线性布局(LinearLayout)[/b][/size]
以单行或单列的形式排列视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="left"
android:layout_weight="1" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"
android:layout_weight="2" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right"
android:layout_weight="3" />
</LinearLayout>


[size=large][b]表格布局(TableLayout)[/b][/size]
以行和列的形式组织视图,每一行可以包含一个或者多个视图,行内的每一个视图构成一个单元格,每一列的宽度由最大单元格的宽度决定

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<TableRow>
<TextView
android:text="User Name:"
android:width ="120dp"
/>
<EditText
android:id="@+id/txtUserName"
android:width="200dp" />
</TableRow>
<TableRow>
<TextView
android:text="Password:"
/>
<EditText
android:id="@+id/txtPassword"
android:password="true"
/>
</TableRow>
<TableRow>
<TextView />
<CheckBox android:id="@+id/chkRememberPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Remember Password"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/buttonSignIn"
android:text="Log In" />
</TableRow>
</TableLayout>


[size=large][b]帧布局(FrameLayout)[/b][/size]
在屏幕上可以用来显示一个单个视图的占位符,添加到FrameLayout中的视图常常锚定在布局的左上方。如果在FrameLayout中添加另一个视图(如button),这个视图将覆盖先前的视图。虽然在FrameLayout可以添加多个视图,但是每一个视图都是堆叠在前一个上面。可以放置一系列图片做成动画。
ScrollView是一种特殊类型的FrameLayout。

<RelativeLayout
android:id="@+id/RLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/lblComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Android!"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lblComments"
android:layout_below="@+id/lblComments"
android:layout_centerHorizontal="true"
>
<ImageView
android:src = "@drawable/droid"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="124dp"
android:layout_height="wrap_content"
android:text="Print Picture" />

</FrameLayout>
</RelativeLayout>


[size=large][b]相对布局(RelativeLayout)[/b][/size]
可用于指定子视图相对于彼此之间如何定位的,由下面一些属性来定位
[list]
[*]layout_alignParentTop
[*]layout_alignParentLeft
[*]layout_alignLeft
[*]layout_alignRight
[*]layout_bleow
[*]layout_centerHorizontal
[/list]


<RelativeLayout
android:id="@+id/RLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/lblComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Android!"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lblComments"
android:layout_below="@+id/lblComments"
android:layout_centerHorizontal="true"
>
<ImageView
android:src = "@drawable/droid"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="124dp"
android:layout_height="wrap_content"
android:text="Print Picture" />

</FrameLayout>
</RelativeLayout>


[size=large][b]绝对布局(AbsoluteLayout)[/b][/size]
已废弃,可以指定其子元素的确切位置

<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:layout_width="188dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="126px"
android:layout_y="361px"
/>
<Button
android:layout_width="113dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="12px"
android:layout_y="361px"
/>
</AbsoluteLayout>


[size=x-large][b]适应显示方向[/b][/size]
当改变Android设备的方向时,当前活动实际上是先被销毁,然后在重新创建。这是因为当显示方向上发生改变时,都会触发活动的onCreate()方法。
通常可以使用下面两种技术来处理屏幕方向的变化
[b]锚定视图.[/b]
将视图锚定到屏幕的四条边是最容易的方法
涉及到的属性有很多,下面举几个常见的
layout_alignParentLeft 将视图与其父视图的左侧对齐
layout_alignParentRight 将视图与其父视图的右侧对齐
layout_centerVertical 将视图在其父视图中水平居中
[b]调整大小和重新定位[/b]
基于屏幕方向定制用户界面的更简单方法是为每个方向上的用户界面创建一个新的视图文件。为了支持横屏模式,可以在res下新建一个文件夹,取名为layout-land

[size=x-large][b]管理屏幕变化方向[/b][/size]

当设备旋转时,活动先被销毁,然后被重新创建。所以对于任何活动。需要在onPause()方法中保存任何你需要保存的状态。当活动被销毁时,只有那些被命名的视图(android:id)才能保持他们的状态。

[size=x-large][b]以编程的方式创业页面[/b][/size]
可以通过xml的方式来拖动来展示页面,也可以通过编程方式创建页面。两者效果一样,但是在工作量上面xml方式更占优势

[size=x-large][b]倾听用户页面通知[/b][/size]
用户和用户页面在两个层面上进行交互,活动层面和视图层面。在活动层面,除了通常的一些方法外还有下面一些常用的方法
onkeyDown() 当键盘按下并且没有被活动中的任何视图处理时调用;
onkeyUp() 当键盘弹起并且没有被活动中的任何视图处理时调用;
onMenuItemSelected() 当用户选择了面板的菜单时调用;
onMenuOpened() 当用户打开了面板的菜单时调用;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值