Android初步(二)


 

Android的初步为一个登陆界面,下面以QQ登录界面为例。登陆界面可以用拖动也可以用手写。拖动比较简单,直接找到要的组件,比如按钮、文本框等。

这里有些名词:Button:  按钮,用于表示登录等   

TextView: 类似于按钮的组件  (两者区别是前者有边框,而后者无边框,因此可用在复选框后。)用于表示账号、密码等

EdiVext :文本框  与Textview相对应,用于输入账号密码

CheckBox:复选框  用于记住密码、自动登录等  

ImageView:图片   用于添加图片

 

而手写的话要稍微复杂一点。

手写步骤如下:

1)、选定布局

首先Android中写法有点像php一样的标记语言,要注意写法与Java中写法的区别。

添加属性都是用如下方式:

添加组件:

<LinearLayout >

        <Button />

        

    </LinearLayout>

 

Android中的布局有多种,且与Java中有一定的区别,在这里主要是用线性布局LinearLayout。如上图,将要添加的组件放于一对线性布局之间,反斜杠表示结束。每个组件都有相应的属性,属性放在一对尖括号之间、反斜杠之前。

线性布局的属性:

<LinearLayout 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

 

 

其中orientation表示方向,有水平(horizontal)、垂直(vertical)。注意Android中的布局相当于Java中的一个容器。若为水平线性布局,当组件太多放不下时后面的组件就不会显示出来。还要注意当要添加多个线性布局时,应先在总的布局中设定方向。当要将该线性布局居中时,应在总的布局中设定如下:

android:gravity="center"

 

设宽和高时,提示会有三种分别是:fill_parentmatch_parentwrap_content。前两者效果相同,后者是根据内容来确定布局的大小。当然还可以自己设定一个大小,方法如下:

 

android:layout_width="200dp"

android:layout_height="30dp"

 

 

其中dp类似于图片中的像素。

 

2)、添加组件

将要添加的组件名放于一对尖括号之间,反斜杠之前。

一般的组件都应有以下几种属性:

Id:即名字,方便以后找到该组件

大小:包括长和宽

 

添加背景图片时应在总的布局中添加:

android:background="@drawable/niguang"

 

以下为添加的一张图片:

<ImageView

        android:id="@+id/im1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/ic_launcher" />

 

 

以下为添加的一个按钮:

 <Button 

        android:id="@+id/but"

        android:layout_width="250dp"

        android:layout_height="40dp"

        android:text="登陆"/>

 

 

源代码如下:

<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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:orientation="vertical"

    android:gravity="center"

    android:background="@drawable/niguang"

    tools:context=".FirstActivity" >

    

    

 

    <ImageView

        android:id="@+id/im1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/ic_launcher" />

 

    <LinearLayout 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

        

        <TextView 

            android:id="@+id/tv1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="账号:"/>

        <EditText 

            android:id="@+id/et1"

            android:layout_width="200dp"

            android:layout_height="30dp"/>

        

    </LinearLayout>

    

    <LinearLayout 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">

        

        <TextView 

            android:id="@+id/tv2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="密码:"/>

        <EditText 

            android:id="@+id/ed2"

            android:layout_width="200dp"

            android:layout_height="30dp"/>

        

    </LinearLayout>

    

    <LinearLayout 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">

        

        <CheckBox 

            android:id="@+id/cb1"

            android:layout_width="30dp"

            android:layout_height="30dp"

            />

        <TextView 

            android:id="@+id/tv3"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="记住密码"

            />

        <CheckBox 

            android:id="@+id/cb2"

            android:layout_width="30dp"

            android:layout_height="30dp"

            />

        <TextView 

            android:id="@+id/tv4"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="自动登录"/>

    </LinearLayout>

    

    <Button 

        android:id="@+id/but"

        android:layout_width="250dp"

        android:layout_height="40dp"

        android:text="登陆"/>

    

    

</LinearLayout>

 

界面截图如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值