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

一个Android登陆/注册XML布局文件代码

2018-01-04 00:00 274 查看
一个Android登陆/注册XML布局文件代码

通常在APP开发中不可避免要涉及到登陆/注册xml布局文件的编码实现,这些Android APP登陆/注册XML布局文件代码倒不是很难,但是要在xml布局写代码实现UI设计要求的各种颜色、大小、字体、间距、圆角等等细节设计要求,那么就比较繁琐了,需要不断的细微调整,同时要考虑视屏和匹配不同的Android设备屏幕。还有就是,这类Android登陆/注册XML布局文件代码基本上可以说复用度很高,基本上一次写好,在未来的项目实际开发中可以重复使用或者略加修改即可再次投入代码中。下面是Du Yong Ping同学写出来的这样一个Android登陆/注册UI之XML布局文件代码实现,我把代码重新抽取和整理出来,使之解耦,以达到未来代码开发中的复用,先代码运行结果:



布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="注册信息"
android:textColor="#f00"
android:textSize="30sp" />

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#f00" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="用户名:"
android:textSize="18sp" />

<EditText
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_background"
android:ellipsize="start"
android:hint="输入用户名"
android:singleLine="true"
android:textSize="18sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="密    码:"
android:textSize="18sp" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_background"
android:ellipsize="start"
android:hint="输入密码"
android:inputType="numberPassword"
android:singleLine="true"
android:textSize="18sp" />
</LinearLayout>

<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="@drawable/button_background"
android:gravity="center"
android:text="注册"
android:textSize="20sp" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="20dp"
android:background="@drawable/scroll_background" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="条款内容\nZhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN Zhang Phil @ CSDN"
android:textColor="@android:color/darker_gray" />
</ScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不同意该协议条款"
android:textSize="12sp" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="同意该协议条款"
android:textSize="12sp" />
</RadioGroup>
</LinearLayout>

</LinearLayout>


布局文件使用到的、在drawable目录下放置的shape文件,有三个,分别是:
用户名/密码输入框的背景资源edit_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<stroke
android:width="2dp"
android:color="#f0f" />

<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />

</shape>


登录/注册按钮的背景资源button_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<stroke
android:width="4dp"
android:color="#0ff" />

<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />

<solid android:color="#0ff" />

</shape>


协议条款scroll用到的scroll_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<stroke
android:width="1dp"
android:color="#456789" />

<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />

</shape>


注意输入框和按钮的背景圆角处理技巧,将shape做成一个矩形(rectangle),然后在四个角(顶部左边topLeft,顶部右边topRight,底部左bottomLeft,底部右bottomRight)的地方处理成圆角。

附录文章:
《仿微信、短信、QQ等消息数目右上角红色小圆球气泡显示(基于Android XML布局文件实现)》链接地址:http://blog.csdn.net/zhangphil/article/details/43702953
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐