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

我的第一个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 )

像这样的



让我们开始吧~

安卓支持使用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.



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>


点击运行 就会像这样



共勉。

更新 如果想变成对齐 譬如这样



可以添加一句 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等分效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: