本文主要介绍通过两种方式来进行界面设计:
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”
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
【附】相关架构及资料
源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,和技术大牛一起讨论交流解决问题。
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-8Qr1AcNL-1711878262722)]
【附】相关架构及资料
源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,和技术大牛一起讨论交流解决问题。
[外链图片转存中…(img-vD0Ektjf-1711878262722)]