Android添加附件组件->登录界面简单实现

创建好一个Android Activity以后,我们可以直接在.xml文件中拖动组件来进行界面的设置。当然拖动的时候不能准确的布局。所以我们还可以在layout中自己写代码添加组件,设置位置大小等等。
一,首先来介绍一下Android的一些组件吧。
打开.xml页面,选择切换到Graphical Layout页面。
这里写图片描述
从第一个开始看,Palette这里可以改变以下组件显示样式,点击右边的小三角就可以进行选择了。
在这里简要介绍几个常用的组件。
1.TextView:用来显示字符串的组件,在手机上就是显示一块文本的区域。
2.EditView:输入框,显示是一条横线,用户可以输入数字字母等。
3.Button:按钮,可点击。
4.imageView:添加图片

二,现在切换到.xml页面,没添加东西前,默认的是这个样子的,它会默认添加一块文本区域,显示hello world!
这里写图片描述

在TextView前面的一部分,

 <ImageView 
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/cat"
        />

2.接下来添加账号文本和输入框,代码如下:

<TextView
        android:id="@+id/zhanghao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/zhanghao" 
        android:textSize="20sp"
        />

    <EditText 
        android:id="@+id/zhanghao_"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFAFA"/>

3.,.接下来添加密码文本和输入框,代码如下:

<TextView 
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password"
        android:textSize="20sp" 
        />

    <EditText 
        android:id="@+id/password_"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFAFA"/>

4.添加登录按钮,代码如下:

<Button 
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login"
        />

5.以上知识添加了必要的组件,最终的登录界面的实现,还需要加上一些布局,这里用的是线性布局LinearLayout。以下是登录界面以及完整的登录界面的.xml中的代码。
这里写图片描述

<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"
    tools:context=".MainActivity" 
    android:orientation="vertical"

    android:gravity="center"
    android:background="@drawable/background_second">

    <ImageView 
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/cat"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:id="@+id/zhanghao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/zhanghao" 
        android:textSize="20sp"
        />

    <EditText 
        android:id="@+id/zhanghao_"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFAFA"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView 
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password"
        android:textSize="20sp" 
        />

    <EditText 
        android:id="@+id/password_"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFAFA"/>
    </LinearLayout>

    <Button 
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login"
        />

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_42882887/article/details/81606559
今日推荐