目录
(3)解决 EditText 随着输入内容增多不断被拉长的问题
(4)可以结合使用EditText 和 Button 完成一些功能
过去Android应用程序的界面主要是通过编写 XML 的方式来实现的。最近几年 Googel 又推出了一个全新的界面布局:ConstrainLayout,ConstrainLayout更适合在可视化编辑器中使用拖放控件的方式来进行操作。
常用控件的使用方法
1、TextView
(1)android:layout_width和android:layout_height 单位dp(一种屏幕密度无关的尺寸单位,可以保证在不同分辨率的手机上显示效果尽量一样)。
(2) match_parent 表示让当前控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小。warp_content 表示让当前控件的大小能够包含里面的内容。
(3)文字默认左上角对齐,可以通过android:gravity修改TextView的对齐方式,可选值有top、buttom、start、end、center等,可以用‘|’来同时指定多个值。如"center"的效果等同于"center_vertical | center_horizontal"。
(4) 此外还可以对TestView中文字的颜色和大小进行修改。android:textSize 属性可以指定文字的大小,使用sp作为单位。
创建一个Hi项目,然后右键 com.example.hi -> New -> Activity -> Empty Activity
在 activity_text_view.xml 中添加 TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ui.TextView">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00ff00"
android:textSize="24sp"
android:gravity="center"
android:text="Hello World!"/>
</LinearLayout>
2、Button
(1) 在 activity_main.xml 中添加button
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
ps: Android 系统默认会将按钮上的英文字母全部转换成大写,可以在xml中添加