UI是User Interface的简称(用户界面)。
这里的工具依旧是eclipse。
首先是创建安卓应用项目。创建好以后,打开在这个项目的res文件的layout文件下xml的文件。打开之后会出现一个界面,如截图。左边的Graphical是可以直接看到样式,右边的xml,在那写代码。
在Graphical可以直接把需要的内容直接拖到右边,对应的xml也会自动生成代码。
布局分为Absolute Layout绝对布局,tableLayout表格布局,FrameLayout帧布局,LineLayout线性布局,RelativeLayout相对布局,GridLayout网格布局。
可以通过右边的outline修改布局。
选中那个相对布局,右键找到更换布局Change Layout,然后单击
然后会跳出这个。选择需要的布局点击ok就行。
接下来写一个用户登陆界面的例子。
默认的是相对布局,就没有改动了。拖入textview,然后可以直接去xml文件修改显示的内容,也可以等完成拖入所有需要的组件再去。
依次拖入text fields中的plain text,textview,text fields中的password还有一个button。textview显示文字以及button显示文字直接修改相应android:text=""双引号内的就行,需要什么就改成什么。
android:layout_width这个是这个组件的宽度,需要修改的话,把 android:layout_width之后的删除,再输入等号,会弹出相应的属性,(一行)。
wrap_content:能够包容
fill_parent:整行填充
match_parent:天充满父容器,具有自动适应。
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ui.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="12dp"
android:layout_y="39dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="-1dp"
android:layout_y="104dp"
android:text="密码:" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="157dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="239dp"
android:text="提交" />
</AbsoluteLayout>