浅谈安卓UI设计「建议收藏」

浅谈安卓UI设计「建议收藏」用户界面在程序开发中十分重要,一个好的用户界面设计需要考虑到用户使用体验、是否美观方便等。在界面设计的过程中,需要考虑如何制作出UI界面,怎么样控制UI界面两大块。这里先放上之前我们UI作业的截图:本文主要介绍通过两种方式来进行界面设计:1、通过xml文件进行界面设计2、通过代码控制进行界面设计一、…

大家好,又见面了,我是你们的朋友全栈君。

用户界面在程序开发中十分重要,一个好的用户界面设计需要考虑到用户使用体验、是否美观方便等。
在界面设计的过程中,需要考虑如何制作出UI界面,怎么样控制UI界面两大块。
这里先放上之前我们UI作业的截图:
1
2
3
4
5
6
本文主要介绍通过两种方式来进行界面设计:
1、通过xml文件进行界面设计
2、通过代码控制进行界面设计

一、通过xml文件进行界面设计
打开Android Studio,建立工程,在res/layout下存放的是界面布局文件。双击创建的文件,左边是界面设计,右边对应了界面设计的xml文本。
1>在左边控件中,拖动一个button到右边的手机界面中,之后点击上线画圈右边的text查看文本,可以看到xml已经编写完成。
2>切换到代码目录,打开之前创建的MainActivity,在onCreate()方法中:

setContentView(R.layout.activity_main);  //将编写的界面显示到手机屏幕

MainActivity添加两个私有数据成员:

private TextView tv;
private Button bt;

onCreate()里面初始化tv和bt,并给bt添加监听事件

tv = (TextView)findViewById(R.id.textView);//控件初始化
bt = (Button) findViewById(R.id.button);//控件初始化

bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("你点击了按钮!");
}
);//添加监听

运行程序,点击按钮,原来的hello world!文本发生改变。在这里,两个控件都是通过xml文件定义的,我们在代码中实现了一个监听器,也就是界面的控制逻辑。
实例:
通过代码进行界面设计时,我们建立一个TextView控件来写标题;建立一个ImageView控件来写标题。先将图片复制到res/drawable目录下,然后通过app:srcCompat=”@drawable/sysu”来引用;建立两个TextView控件,用来写“学号”和“密码”,设置建立两个EditView控件,用来输入学号和密码;建立一个RadioGroup,之后再在里面建立两个单选按钮RadioButton。
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.yc.sysu.MainActivity">

    <TextView  android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学生信息系统" android:textSize="20sp" android:textColor="#000000" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="20dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/>

    <ImageView  android:id="@+id/icon" android:layout_width="104dp" android:layout_height="104dp" app:srcCompat="@drawable/sysu" app:layout_constraintTop_toBottomOf="@id/title" android:layout_marginTop="20dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" />

    <TextView  android:id="@+id/user_id" android:text="学号:" android:textColor="#000000" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginLeft="20dp" app:layout_constraintTop_toBottomOf="@id/icon" android:layout_marginTop="20dp" />

    <TextView  android:id="@+id/user_pwd" android:text="密码:" android:textColor="#000000" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginLeft="20dp" app:layout_constraintTop_toBottomOf="@id/user_id" android:layout_marginTop="20dp"/>

    <EditText  android:id="@+id/text_userid" android:hint="请输入学号" android:textColor="#000000" android:textSize="18sp" android:paddingTop="0dp" android:digits="0123456789" android:layout_width="fill_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="@id/user_id" app:layout_constraintLeft_toRightOf="@+id/user_id" app:layout_constraintRight_toRightOf="parent" android:layout_marginRight="20dp"/>

    <EditText  android:id="@+id/text_userpwd" android:hint="请输入密码" android:textColor="#000000" android:textSize="18sp" android:password="true" android:paddingTop="0dp" android:layout_width="fill_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="@id/user_pwd" app:layout_constraintLeft_toRightOf="@+id/user_pwd" app:layout_constraintRight_toRightOf="parent" android:layout_marginRight="20dp" />

    <RadioGroup  android:id="@+id/radioButton" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/user_pwd" android:layout_marginTop="30dp">

    <RadioButton  android:id="@+id/radioButton1" android:text="学生" android:textColor="#000000" android:textSize="18sp" android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

    <RadioButton  android:id="@+id/radioButton2" android:text="教职工" android:textColor="#000000" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp"/>
    </RadioGroup>

    <View  android:id="@+id/button_box" android:layout_height="50dp" android:layout_width="185dp" app:layout_constraintTop_toBottomOf="@id/radioButton" android:layout_marginTop="20dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/>

    <Button  android:id="@+id/button1" android:text="登录" android:textColor="#ffffff" android:background="@drawable/shape" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="@id/button_box" app:layout_constraintTop_toTopOf="@id/button_box" />

    <Button  android:id="@+id/button2" android:text="注册" android:textColor="#ffffff" android:background="@drawable/shape" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toRightOf="@id/button1" android:layout_marginLeft="10dp" app:layout_constraintTop_toTopOf="@id/button_box"/>


</android.support.constraint.ConstraintLayout>

shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#3f51b5"/>
    <corners android:radius="10dip"/>
    <padding  android:bottom="5dp" android:top="5dp" android:left="10dp" android:right="10dp"/>
</shape>

二、通过代码进行界面设计
定义MainActivity的私有成员:

    private TextView tv;
    private Button bt;

重写onCreate(),通过new定义一个线性布局和Button按钮和文本框控件,布局里面加入控件,控件加上监听事件

LinearLayout l = new LinearLayout(this);  //定义线性布局
        setContentView(l);                //线性布局加入屏幕

        tv = new TextView(this);          //定义控件
        bt = new Button(this);            //定义控件

        l.addView(bt);                   //加入布局
        l.addView(tv);                  //加入布局

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("你点击了按钮!");
            }
        });         //监听事件

运行代码,点击按钮,将会出现”你点击了按钮!”的文本提示。

三、总结
在界面设计前,需要先调查用户需求,再进一步展开设计。安卓界面设计通过xml文件进行界面设计,这是一种十分高效的方式。通过代码进行界面设计则是一种更为灵活的开发方式。在实际操作中,两者结合能够大大简化界面设计工作。

作者:陈语童
原文链接:https://blog.csdn.net/weixin_41402015/article/details/80716263

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149381.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Origin简单绘图

    Origin简单绘图一、从cadence导出数据仿真生成波形之之后,鼠标选中波形,右击—>SendTo—>Export,进行csv数据的保存。打开该csv文件,删掉第一行,第一行是是横纵坐标的标识,左侧第一列是横坐标值,右侧列是纵坐标值。二、origin简单绘图双击图标打开origin导入csv数据可选中多个csv文件导入为了让两个csv的数据同时显示,在弹出的对话框进行以下操作(默认第二个csv数据会覆盖第一个csv数据)。设置好之后点击“确定”,两个csv数据均被导入到了o

    2022年6月1日
    35
  • 【Matlab】如何规范地编写一个MATLAB函数文件

    【Matlab】如何规范地编写一个MATLAB函数文件在matlab中,M文件分为脚本文件和函数文件。如果M文件的第一个可执行语句以function开头,那这个M文件就是函数文件。函数文件内定义的变量为局部变量,只在函数文件内部起作用,当函数文件执行完后,这些内部变量将被清除。本文介绍如何规范地编写一个函数文件。通常,函数文件由函数声明行、H1行、在线帮助文本区、编写和修改记录、函数主体等几个部分组成。格式如下:function输出形参…

    2022年7月17日
    10
  • bz2压缩解压「建议收藏」

    bz2压缩解压「建议收藏」压缩bzip2-zfileName解压bzip2-dfileName.bz2bunzip2fileName压缩成.tar.bz2tar-jcvffileName.tar.bz2fileName解压.tar.bz2tar-jxvffileName.tar.bz2

    2022年6月3日
    35
  • idea进入方法快捷键详情大全(idea快捷键大全最新设置)[通俗易懂]

    idea进入方法快捷键详情大全(idea快捷键大全最新设置)[通俗易懂]首页>软件应用>返回首页idea进入方法快捷键详情大全(idea快捷键大全最新设置)软件应用发布时间:2022-02-1311:05:09刚开始使用IDEA时一直都不熟悉,利用空闲的时间整理了一下常用的IDEA快捷键。1、Ctrl快捷键介绍Ctrl+F在当前文件进行文本查找Ctrl+R在当前文件进行文本替换Ctrl+Z撤销Ctrl+Y删除光标所在行或删除选中的行Ctrl+X剪切光标所在行或剪切选择内容…

    2022年10月2日
    0
  • debian9.5安装教程_debian11安装教程

    debian9.5安装教程_debian11安装教程IPSET是Linux内核中用来建立、维护和查看IP集合的工具,常常与iptables配合使用。在Debian-5.0.3下安装ipsetv2.3.3,遇到了一些问题,最终安装成功。将安装过程及遇到的问题和大家分享。 1安装环境Debian-5.0.3,安装过程中选择内核为2.6.26-2-486。 2用Debian的包管理器aptitude安装ipset安装完成以后

    2022年9月1日
    2
  • 1.什么是线程池?为什么要使用线程池

    1.什么是线程池?为什么要使用线程池线程池作为java开发者必备技能,在日常开发中十分常见,所以从这一节开始我们介绍线程池。今天的内容有两点,第一点,为什么要使用线程池?第二点,什么是线程池?首先来看第一点,为什么要使用线程池?在

    2022年7月3日
    28

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号