一、常用组件
1. TextView
-
android: layout_width
和android: layout_height
是所有组件都有的属性,用于指定控件宽高,可选的值有:match_ parent
表示让当前控件的大小和父布局的大小-样,也就是由父布局来决定当前控件的大小。fill_parent
和match_parent
的意义相同,现在官方更加推荐使用match_parent
。wrap_content
表示让当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小。
-
android:gravity
指定文字的对齐方式,可选值有:top
、bottom
、left
、right
、center
等,可以用|
来同时指定多个值。 -
通过
android: textSize
属性可以指定文字的大小,通过android:textColor
属性可以指
定文字的颜色,在Android
中字体大小使用sp
作为单位。
2. Button
Button
和TextView
的基本属性差不多。系统会对Button
中的所有英文字母自动进行大写转换。如果想要取消这种效果,可以添加修改xml
文件中的配置:
<Button
android: id="@+id/button"
android: layout_ width=" match_parent"
android: layout_ height= "wrap_content "
android: text="Button"
android: textAllCaps="false" />
<!-- 使用 android: textAllCaps="false" 可以取消大写 -->
为Button
添加监听事件
使用匿名类方式:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState) ;
setContentView(R.layout.activity_ main) ;
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//在此处添加逻辑
}
});
}
}
使用实现接口方式:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@0verride
protected void onCreate ( Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout.activity_main) ;
Button button = (Button)findViewById (R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
//在此处添加逻辑
break;
default:
break;
}
}
}
3. EditText
用户可以在控件中输入和编辑内容,并可以在程序中对这些内容进行处理。
设置提示信息
在js
中,输入框可以设置提示信息,Android
也可以。
<LinearLayout xmlns :android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android: layout_width="match_parent"
android: layout_height= "wrap_content"
android: hint="Type something here"
/>
<!-- 使用 android: hint="Type something here" 可以添加提示信息 -->
</Linearlayout>
设置最长文本限制
与js
类似,还可以设置文本的最大长度。
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android: id="@+id/edit_text"
and roid: layout_width="match_ parent"
and roid: layout_height="wrap_ content"
android:hint="Type something here"
android: maxLines="2"
/>
<!-- 使用 android: maxLines="2"设置最长文本,超过限制,文本会向上滚动 -->
</Linearlayout>
获取EditText
中的内容
通过getText()
可以获取输入的内容,调用toString()
方法可以转换成字符串。
4. ImageView
ImageView
用于在页面中显示图片。图片通常放在drawable
开头的目录下。
<LinearLayout xmlns :android=" http://schemas.android.com/apk/res/android"
android: orientation="vertical"
android: layout_width=" match_parent"
android: layout_height="match_parent">
<ImageView
android: id="@+id/image_view"
android: layout_width: ="wrap_content"
android: layout_height="wrap_content"
android: src="@drawable/img_1"
/>
</Linearlayout>
设置图片源
调用组件的setImageResource()
方法可以修改组件的图片源。
imageView.setImageResource(R.drawable.img_2);
5. ProgressBar
ProgressBar
用于在界面中显示一个进度条,表示我们的程序正在加载一些数据。
<LinearLayout xmlns : android="http://schemas.android.com/apk/res/android"
android: orientat ion="vertical"
android: layout_width=" match_parent"
android: layout_height= "match_parent">
<ProgressBar
android: id="@+id/progress_bar"
android: layout_width: ="match_parent"
android: layout_height="wrap_content"
android:visibility = "invisible"
style="?android: attr/progressBarStyleHorizontal"
android : max="100"
/>
</LinearLayout>
当数据加载完成时,进度条需要消失。可以修改android:visibility
实现(所有控件都有)。它有 3 中可选值:
visible
表示控件是可见的,这个值是默认值,不指定android:visibility
时,控件都是可见的。invisible
表示控件不可见,但是它仍然占据着原来的位置和大小,可以理解成控件变成透明状态了。gone
则表示控件不仅不可见,而且不再占用任何屏幕空间。
还可以通过代码来设置控件的可见性,使用的是setVisibility()
方法,可以传入View.VISIBLE
、View.INVISIBLE
和View.GONE
这3种值。
progressBar.setVisibility(View.VISIBLE);
而获取可见性则通过getVisibility()
。另外,我们还可以给ProgressBar
指定不同的样式,刚刚是圆形进度条,通过style
属性可以将它指定成水平进度条,还可以通过android:max
属性给进度条设置一个最大值。
除此以外,还可以动态设置进度条的值。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// 。。。。。。。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
// 获取数值
int progress = progressBar.getProgress();
progress = progress + 10;
// 设置数值
progressBar.setProgress(progress);
break;
default:
break;
}
}
}
6. AlertDialog
AlertDialog
可以在当前的界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertDialog
一般都是用于提示一些非常重要的内容或者警告信息。比如为了防止用户误删重要内容,在删除前弹出-一个确认对话框。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
ALertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is Dialog");
dialog.setMessage("Something important.");
// 是否可取消
dialog.setCancelable(false) ;
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface dialog, int which) {
});
dialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
@0verride
public void onClick (DialogInterface dialog, int which) {
});
dialog.show();
break;
default:
break;
}
}
7. ProgressDialog
ProgressDialog
和AlertDialog
有点类似,都可以在界面上弹出一个对话框,都能够屏蔽掉其他控件的交互能力。不同的是,ProgressDialog
会在对话框中显示一个进度条,一般用于表示当前操作比较耗时,让用户耐心地等待。它的用法和AlertDialog
也比较相似。
public class MainActivity extends AppCompatActivity implements View. OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R. id. button:
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("This is ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true) ;
progressDialog.show();
break;
default:
break;
}
}
注意,如果在
setCancelable()
中传人了false
, 表示ProgressDialog
是不能通过Back键
取消掉的,这时你就一定要在代码中做好控制,当数据加载完成后必须要调用ProgressDialog
的
dismiss ()
方法来关闭对话框,否则ProgressDialog
将会一直存在。
二、自定义控件
首先需要知道自定义控件如何使用,一般可以通过以下方式:
在布局文件中引用布局文件
<!-- xxx表示布局文件名称 -->
<include layout="@layout/xxx" />
<!-- 或者使用包名 -->
<com.example.customview.TitleView
android:id="@+id/title_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.customview.TitleView>
自绘控件
View
上所展示的内容全部都是自己绘制出来的。创建一个自定义控件,继承自View
,重载绘制函数onDraw()
,将想要显示出来的内容在函数中绘制。
组合控件
将原生的控件进行组合形成自己的组合控件。
继承控件
继承已有的控件,向这个控件添加新功能,形成新的控件。
涉及到的知识太多、太底层,有些看不懂。
参考原文链接
三、开源框架
网上有许多针对不同小功能的UI框架
,也有一些比较综合的UI框架
,比较出名的大公司的AndroidUI框架
:
-
腾讯的QMUI Android
-
Google 公司的Flutter
(不知道算不算,并不是只用于Android的UI框架,但是很牛逼)
-
Bootstrap风格的Android UI框架Android-Bootstrap
-
Flat UI 风格的Android UI 框架android-flat-button
参考文章:
四、基本布局
1. LinearLayout
(线性布局)
线性——可以水平也可以垂直布局。当android:orientation
为vertical
时,布局排列方向时垂直的;当其为horizontal
时,控件就会在水平方向上排列。
android:layout_gravity
与android:gravity
的区别
android:gravity
用于指定文字在控件中的对齐方式,而android: layout_gravity
用于指定控件在布局中的对齐方式。android: layout_gravity
的可选值和android:gravity
差不多,但是需要注意,当LinearLayout
的排列方向是horizontal
时,只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。同样的道理,当LinearLayout
的排列方向是vertical
时,只有水平方向上的对齐方式才会生效。
按比例布局
android: layout_weight
用于指定控件在布局中所占比例。系统会先把LinearLayout
下所有控件指定的layout_ weight
值相加,得到一个总值,然后每个控件所占大小的比例就是用该控件的layout_weight
值除以刚才算出的总值得到所占比例。
小技巧:我们可以设置一个控件的大小按内容(
wrap_content
),另一个控件则按比例占用,这样看起来组件大小更自然不生硬。
2. RelativeLayout
(相对布局)
他可以让控件相对于某个控件出现在布局的任何位置,可以相对于布局控件,也可以相对于布局控件中的控件。
相对于整个布局
控件可以设置的属性有:
android:layout_alignParentLeft
android: layout_alignParentTop
android: layout_alignParentRight
android: layout_alignParentBottom
android:layout_centerInParent
这几个属性我们之前都没接触过,可是它们的名字已经完全说明了它们的作用。
相对于控件
android: layout_above
指定相对布局的控件。
<RelativeLayout xmlns: android=" http: //schemas.android.com/apk/res/android"
android: layout_width=" match_parent "
android: layout_height="match_parent">
<Button
android: id="@+id/button3"
android: layout_width= "wrap_content "
android: layout_height="wrap_content "
android: layout_centerInParent="true"
android: text="Button 3" />
<Button
android: id="@+id/button1"
android: layout_width="wrap_content"
android: layout_height= "wrap_content "
android: layout_above="@id/button3"
android: layout_toLeft0f="@id/button3"
android: text="Button 1" />
</RelativeLayout>
3. FrameLayout
(帧布局)
这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角。
4. TableLayout
(表格布局)
每个TableLayout
,都由多个TableRow
组成,每个TableRow
就是一行,有几个TableRow
就有几行。
-
TableLayout
的android:shrinkColumns
属性,当TableRow
里边的空间布满布局的时候,指定列自动延伸以填充可用部分。当TableRow
里边的控件还没有布满布局时,android:shrinkColumns
不起作用。当TableLayout
设置了android:shrinkColumns
属性,则在TableRow
中的控件如果超长的话,设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。 -
TableLayout
的android:stretchColumns
属性,用于指定列对空白部分进行填充。若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。 -
TableLayout
的android:collapseColumns
属性,用于隐藏指定的列,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。 -
TableLayout
的android:layout_span
属性,设置组件显示所占用的列数。 -
TableLayout
的android:layout_column
属性,设置组件所在列数。
5. AbsoluteLayout
(绝对布局)
不常使用,不能很好地适配设备。~~(有点像HTML中的绝对定位)~~常用属性有:
控制大小:
android:layout_width
:组件宽度; android:layout_height
:组件高度
控制位置:
android:layout_x
:设置组件的X
坐标 ;android:layout_y
:设置组件的Y
坐标
6. GridLayout
(网格布局)
网格布局有点类似于表格布局。但是也有区别,相比表格布局,它有以下优点:
- 可以自己设置布局中组件的排列方式
- 可以自定义网格布局有多少行,多少列
- 可以直接设置组件位于某行某列
- 可以设置组件横跨几行或者几列
因为
GirdLayout
是4.0
后才推出的,所以SDK版本
要改为14或者以上的版本。
GridLayout 使用属性
属性 | 作用 |
---|---|
android:columnCount | 最大列数 |
android:rowCount | 最大行数 |
android:orientation | GridLayout中子元素的布局方向 |
android:alignmentMode | alignBounds:对齐子视图边界 alignMargins :对齐子视距内容,默认值 |
android:columnOrderPreserved | 使列边界显示的顺序和列索引的顺序相同,默认是true |
android:rowOrderPreserved | 使行边界显示的顺序和行索引的顺序相同,默认是true |
android:useDefaultMargins | 没有指定视图的布局参数时使用默认的边距,默认值是false |
item属性
属性 | 作用 |
---|---|
android:layout_column | 指定该单元格在第几列显示 |
android:layout_row | 指定该单元格在第几行显示 |
android:layout_columnSpan | 指定该单元格占据的列数 |
android:layout_rowSpan | 指定该单元格占据的行数 |
android:layout_gravity | 指定该单元格在容器中的位置 |
android:layout_columnWeight | (API21加入)列权重 |
android:layout_rowWeight | (API21加入) 行权重 |
android:layout_gravity | 作用 |
---|---|
center | 不改变元素的大小,仅居中 |
center_horizontal | 不改变大小,水平居中 |
center_vertical | 不改变大小,垂直居中 |
top | 不改变大小,置于顶部 |
left | 不改变大小,置于左边 |
bottom | 不改变大小,置于底部 |
right | 不改变大小,置于右边 |
start | 不改变大小,根据系统语言,置于开始位置 |
end | 不改变大小,置于结尾 |
fill | 拉伸元素控件,填满其应该所占的格子 |
fill_vertical | 仅垂直方向上拉伸填充 |
fill_horizontal | 仅水平方向上拉伸填充 |
clip_vertical | 垂直方向上裁剪元素,仅当元素大小超过格子的空间时 |
clip_horizontal | 水平方向上裁剪元素,仅当元素大小超过格子的空间时 |
7. PercentxxxxLayout
(百分比布局)
由于LinearLayout
本身己经支持按比例指定控件的大小了,因此百分比布局只为FrameLayout
和RelativeLayout
进行了功能扩展,提供了PercentFrameLayout
和PercentRelativeLayout
.
-
layout_widthPercent
设置控件宽度为父容器的宽的百分比
-
layout_heightPercent
设置控件高度为父容器的高的百分比
-
layout_marginPercent
-
layout_marginLeftPercent
设置控件与左边控件的距离为父容器的宽度的百分比
-
layout_marginTopPercent
设置控件与上方控件的距离为父容器的高度的百分比
-
layout_marginRightPercent
设置控件与右边控件的距离为父容器的宽度的百分比
-
layout_marginBottomPercent
设置控件与下方控件的距离为父容器的高度的百分比
-
layout_marginStartPercent
与上面的说明类似
-
layout_marginEndPercent
与上面的说明类似