Andriod开发学习-Building a Simple User Interface

Add a Text Field

<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">
    <EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
</LinearLayout>

android:id
    The at sign ( @) is required when you're referring to any resource object from XML. It is followed by the resource type ( id in this case), a slash, then the resource name ( edit_message).
    +号用于第一次定义此资源ID,编译器在编译时将生成一个新的ID,在定义此ID之后,对ID的其他引用不再需要+号

    "wrap_content" 充满整个view
    "match_parent"充满父辈的尺寸

android:hint(hint 暗示)
    当文本是空的时候显示默认字符串

    "@string/edit_message" 此处不加+号,string是个实体资源,但是之前没有声明,因此编译会报错,下一布添加对string的声明

Add String Resources
默认情况下,安卓工程包含string源文件res/values/strings.xml,这里添加两个新的string
1. From the res/values/ directory, open strings.xml
2. Add two strings so that your file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
</resources>

对于用户界面中的文本,始终将每个字符串指定为资源。 字符串资源允许您在单个位置管理所有UI文本,这使得文本更易于查找和更新。 对字符串进行外部化还允许您通过为每个字符串资源提供备选定义来将您的应用程序本地化为不同的语言。

Add a Button
添加如下代码    

<Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/button_send" />

注意:这个button不需要 android:id属性,因为它不会被Activity引用
权重的问题
这适用于按钮,但不适用于文本字段,因为用户可能会输入更长的内容。用文本字段填充未使用的屏幕宽度将会很好。您可以使用weight属性在LinearLayout中执行此操作,您可以使用android:layout_weight属性指定该属性。

权重值是一个数字,指定每个视图应该消耗的剩余空间量,相对于兄弟视图消耗的数量。这种方法就像饮料配方中的成分含量:“2份苏打水,1份糖浆”意味着三分之二的饮料是苏打水。例如,如果您给一个视图的权重为2,另一个权重为1,则总和为3,因此第一个视图占用剩余空间的2/3,第二个视图填充剩余空间。如果添加第三个视图并将其权重设为1,则第一个视图(重量为2)现在获得剩余空间的1/2,而其余两个视图获得1/4。

所有视图的默认权重为0,因此如果您只为一个视图指定大于0的任何权重值,那么在所有视图被赋予所需空间之后,该视图将填充剩余空间


Make the Input Box Fill in the Screen Width
In activity_main.xml, modify the <EditText> so that the attributes look like this:

<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

将宽度设置为零(0dp)可以提高布局性能,因为使用“wrap_content”作为宽度要求系统计算最终不相关的宽度,因为重量值需要另一个宽度计算来填充剩余空间。

参考网址:http://androiddoc.qiniudn.com/training/basics/firstapp/building-ui.html#Weight


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值