现在的位置: 首页 > 综合 > 正文

第一课 建立我的第一个安卓程序(not hello world !)

2017年10月20日 ⁄ 综合 ⁄ 共 1575字 ⁄ 字号 评论关闭

谷歌官方的教程

http://developer.android.com/intl/zh-CN/training/basics/firstapp/building-ui.html#Button

 首先简单的建立一个安卓工程

1、创建一个Linear Layout ,修改activity_main.xml

     删除<TextView>,修改<RelativeLayout>,添加android:orientatio

<?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>

2、添加一个文本框

  添加<EditText>

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

3、添加字符的源

 如果你现在编译的话,会出现错误,找不到Resource,找到string.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="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>


</resources>

4、添加一个按钮

在activety_main.xml中添加:

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

 此时,你可编译了

效果图如下:

总结:string.xml 是ui acrtive_main.xml的源文件;android:layout_width和android:layout_height不是使用特定尺寸的宽度和高度, "wrap_content"值指定该视图仅应该是一样大的需要,以适应的内容的视图。 如果你要使用"match_parent"然后EditText元素将充满整个屏幕,因为它的大小相匹配的父LinearLayout

 LinearLayout视图组( ViewGroup子类),垂直或水平方向,由android:orientation属性。 每个孩子的LinearLayout出现在屏幕上的顺序出现在XML中。其他两个属性, android:layout_width和android:layout_height ,需要各方面的意见,以指定其大小。因为LinearLayout的根视图的布局,应填写到"match_parent"设置的宽度和高度可用的应用程序的整个屏幕区域。 该值声明,视图应该拓展它的宽度或高度,以配合与父视图的宽度或高度。

抱歉!评论已关闭.