您的位置:首页 > 移动开发 > Android开发

Android入门之构建简单的用户界面

2014-01-24 11:48 555 查看
这里继续前面的例子。

1、首先看到布局文件activity_main.xml(“res/layout/”目录),在这个文件中默认使用的是一个相对布局(RelativeLayout)。

删除掉文件中
<TextView>这个元素,然后将相对布局修改为线性布局(
LinearLayout
),其中线性布局中可以设置布局的方向:水平(horizontal)和垂直(vertical),通过属性[code]android:orientation
来设置。代码例子如下:[/code]

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="horizontal" > </LinearLayout>

其中两个属性:
android:layout_width


android:layout_height


android:layout_width:布局的宽度,默认是“match_parent”(扩大宽度与父容器宽度匹配);

android:layout_height:布局的高度,默认是“match_parent”(扩大高度与父容器高度匹配);

2、然后在布局中增加一个
<EditText>
<Button>
元素,用来编辑文本,代码如下:


<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/button_send" />

属性描述:

android:id
:此视图的唯一标识,你可以通过id在代码中引用此对象,例如读取/操作一个对象,符号(
@
)当你引用任何的资源的对象的时候需要使用到此符号;加符号(
+
)表示当资源首次定义资源ID时需要。

android:layout_width
and

android:layout_height
代替特定width和height来指定大小

String 资源strings.xml主要用来解决硬编码问题,将所有的静态不可变字符串通过配置资源的方式来配置。

例如代码如下:

<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>

3、使输入框填满屏幕的宽度

通过android:layout_weight="1"权重属性来计算view占用的比率。这个权重值计算每个view应该消耗的控件

android:layout_width="0dp"设置宽度为零,可以改善布局性能,使用“wrap_content”宽度要求系统计算宽度,因为权重值需要另一个宽度计算填充剩余的空间。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: