我的第一个ANDROID APP (一)
2014-02-25 17:31
281 查看
来自
http://developer.android.com/training/basics/firstapp/building-ui.html
我使用的是 Eclipse + android4.4
刚刚开启ANDROID的学习,如果有不足与错误,欢迎大家指正
首先建立一个简单的用户界面 (当然再次之前你需要创建一个android的工程 Flie-New -Project-Android-Android Application Project )
像这样的
![](http://developer.android.com/images/training/firstapp/edittext_wrap.png)
让我们开始吧~
安卓支持使用XML来建立对应的子类的视图和试图组。简单的说就是用XML来规划视图界面
Android provides an XML vocabulary that corresponds to the subclasses of View and ViewGroup so you can define your UI in XML using a hierarchy of UI elements.
![](http://developer.android.com/images/viewgroup.png)
1.Open the activity_main.xml file from the res/layout/ directory.
打开在res/layout/directory的activity_main.xml
之后再点击左下角切换标签页 activity_main.xml
首先,删除<TextView>元素 然后把<RelativeLayout> 改成 <LinearLayout>。就改成像下面这样
android:orientation = "horizontal" 这句话是保证横向定位用的(应该是这样- -!)
android:layout_width and android:layout_height, 这两个就是所需视图的大小
2.要创建一个用户可编辑的文本字段,添加一个<EditText>里面的元素<LinearLayout>。
像每一个视图对象,你必须定义某些XML属性来指定的EditText对象的属性。下面是你应该在里面声明它<LinearLayout>元素:
具体为什么要@+id/edit_message 原文是这样写的
This provides a unique identifier for the view, which you can use to reference the object from your app code, such as to read and manipulate the object (you'll see this in the next lesson).
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).
The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource
ID in your project's gen/R.java file that refers to the EditText element. Once the resource ID is declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not
needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.
3.当我们要加个文本在用户界面的时候,我们总应该加入一个与其对应的资源 When you need to add text in the user interface, you should always specify each string as a resource.
一般情况下,打开res/values/strings.xml 添加一个name为 “edit_message”并将其value设置为“Enter a message”的字符串 .我的看起来像这样,可能会比你多一点
4.再添加一个按钮 加在刚才的EditText 后面,像这样
点击运行 就会像这样
![](http://developer.android.com/images/training/firstapp/edittext_wrap.png)
共勉。
更新 如果想变成对齐 譬如这样
![](http://developer.android.com/images/training/firstapp/edittext_gravity.png)
可以添加一句 android:layout_weight = "1" 在<EditText>中 像这样
快试试把~ 在button中也可以添加 控制比例用 比如4:1 3:1等分效果
http://developer.android.com/training/basics/firstapp/building-ui.html
我使用的是 Eclipse + android4.4
刚刚开启ANDROID的学习,如果有不足与错误,欢迎大家指正
首先建立一个简单的用户界面 (当然再次之前你需要创建一个android的工程 Flie-New -Project-Android-Android Application Project )
像这样的
![](http://developer.android.com/images/training/firstapp/edittext_wrap.png)
让我们开始吧~
安卓支持使用XML来建立对应的子类的视图和试图组。简单的说就是用XML来规划视图界面
Android provides an XML vocabulary that corresponds to the subclasses of View and ViewGroup so you can define your UI in XML using a hierarchy of UI elements.
![](http://developer.android.com/images/viewgroup.png)
1.Open the activity_main.xml file from the res/layout/ directory.
打开在res/layout/directory的activity_main.xml
之后再点击左下角切换标签页 activity_main.xml
首先,删除<TextView>元素 然后把<RelativeLayout> 改成 <LinearLayout>。就改成像下面这样
<?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:orientation = "horizontal" 这句话是保证横向定位用的(应该是这样- -!)
android:layout_width and android:layout_height, 这两个就是所需视图的大小
2.要创建一个用户可编辑的文本字段,添加一个<EditText>里面的元素<LinearLayout>。
像每一个视图对象,你必须定义某些XML属性来指定的EditText对象的属性。下面是你应该在里面声明它<LinearLayout>元素:
<?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" > <EditText android:id = "@+id/edit_message" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:hint = "@string/edit_message" /> </LinearLayout>
具体为什么要@+id/edit_message 原文是这样写的
This provides a unique identifier for the view, which you can use to reference the object from your app code, such as to read and manipulate the object (you'll see this in the next lesson).
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).
The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource
ID in your project's gen/R.java file that refers to the EditText element. Once the resource ID is declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not
needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.
3.当我们要加个文本在用户界面的时候,我们总应该加入一个与其对应的资源 When you need to add text in the user interface, you should always specify each string as a resource.
一般情况下,打开res/values/strings.xml 添加一个name为 “edit_message”并将其value设置为“Enter a message”的字符串 .我的看起来像这样,可能会比你多一点
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">hello</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="edit_message">Enter a message</string> <string name="button_send">Send</string> <string name="title_activity_display_message">DisplayMessageActivity</string> </resources>
4.再添加一个按钮 加在刚才的EditText 后面,像这样
<?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" > <EditText android:id = "@+id/edit_message" android:layout_width = "0dp" 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" /> </LinearLayout>
点击运行 就会像这样
![](http://developer.android.com/images/training/firstapp/edittext_wrap.png)
共勉。
更新 如果想变成对齐 譬如这样
![](http://developer.android.com/images/training/firstapp/edittext_gravity.png)
可以添加一句 android:layout_weight = "1" 在<EditText>中 像这样
<EditText android:id = "@+id/edit_message" android:layout_weight = "1" android:layout_width = "0dp" android:layout_height = "wrap_content" android:hint = "@string/edit_message" />
快试试把~ 在button中也可以添加 控制比例用 比如4:1 3:1等分效果
相关文章推荐
- android 联系人操作: ContentProvider往通讯录添加联系人和获取联系人
- Android 如何去除Launcher上默认的google search bar?
- Android中Path类的lineTo方法和quadTo方法画线的区别
- ios 中介者模式
- ios 自己编写的一个弹球小游戏
- 获取手机端验证码
- IOS6、IOS7导航栏适配问题
- 获取手机端验证码
- iOS push原理
- [Android] 安裝 PhoneGap 3.3 流程(及問題排除)
- Android 信号处理面面观 之 信号定义、行为和来源
- 【微信公众平台】微信营销推广的12种方法,让您的订单增加两倍以上!
- Android的进程、线程模型
- unity ------- input
- MBProgressHUD的使用-ios
- 获取json数据,低版本android无法解析
- Android onCreate, onSaveInstanceState和onRestoreInstanceState与跳转的问题
- Android中的人脸检测(静态和动态)
- 我的学习之旅:android文件下载功能的实现
- 微信账号