| textDirection | 从左到右使用TextDirection.ltr,从右到左使用TextDirection.rtl |
| textScaleFactor | 字体缩放系数,如:如果此属性设置的值为1.5,那么字体会放大150% |
| textSpan | 文本块,TextSpan里可以包含文本内容及样式 |
| data | 要显示的文本 |
| maxLines | 文本显示的最大行数 |
| style | 文本样式,可定义文本的字体大小、颜色、粗细等 |
TextStyle style
| 属性 | 说明 |
| — | :-: |
| TextDecoration decoration | 绘制文本装饰;下划线(TextDecoration.underline)上划线(TextDecoration.overline)删除线(TextDecoration.lineThrough)无(TextDecoration.none) |
| Color decorationColor | 绘制文本装饰的颜色 |
| FontStyle fontStyle | 字体变体:FontStyle.italic 使用斜体FontStyle.normal 使用直立 |
| double height | 文本行与行的高度,作为字体大小的倍数(取值1~2,如1.2) |
| Locale locale | 此属性很少设置,用于选择区域特定字形的语言环境 |
构造方法1:用于创建一个单一格式的 Text
Text(String data, { Key key, TextStyle style, TextAlign textAlign, TextDirection textDirection, Locale locale, bool softWrap, TextOverflow overflow, double textScaleFactor, int maxLines, String semanticsLabel })
使用构造方法1,对上述属性进行运用,示例如下:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Paint backgroundPaint = new Paint();
backgroundPaint.color = new Color(0xFFFFFFFF);
List shadowList = new List();
shadowList.add(new Shadow(
//阴影颜色
color: new Color(0xFFFF0000),
//模糊程度
blurRadius: 2.0));
return MaterialApp(
debugShowCheckedModeBanner: false,
title: ‘Text widget’,
home: Scaffold(
appBar: new AppBar(
title: new Text(‘Text widget’),
),
body: new Text(
//单一格式文本框,第一个参数为文本内容
“我是一段测试文本Hello World!我是一段测试文本 Hello World!我是一段测试文本 Hello World!我是一段测试文本 Hello World!我是一段测试文本 Hello World!”,
//文字对齐方式,文本开始位置对齐,类似左对齐
textAlign: TextAlign.start,
//文字方向
textDirection: TextDirection.ltr,
//文字最多显示行数
maxLines: 3,
//当文本内容超过最大行数时,剩余内容的显示方式,相当于Android 的 ellipsize 属性
overflow: TextOverflow.ellipsis,
//文本样式
style: new TextStyle(
//文字颜色,值为一个 Color 对象
color: new Color(0xFF000000),
//添加装饰物:下划线
decoration: TextDecoration.underline,
//设置装饰物的颜色,值为一个 Color 对象
decorationColor: new Color(0xFF00FFFF),
//设置装饰物的样式,装饰线为虚线
decorationStyle: TextDecorationStyle.dashed,
//字体大小
fontSize: 20.0,
//字体样式:斜体
fontStyle: FontStyle.italic,
//字体字重,常用值有 FontWeight.bold(加粗)
fontWeight: FontWeight.bold,
inherit: false,
//字间距,值为 double 类型
letterSpacing: 2.0,
//文字阴影,值为一个 Shadow 集合
shadows: shadowList,
//文本基线:用于对齐字母字符的字形底部的水平线
textBaseline: TextBaseline.ideographic,
//字体间距
wordSpacing: 10.0,
//行高
height: 2.0),
),
),
);
}
}
运行结果如下:
构造方法2:用于创建一个富文本的 Text
Text.rich(TextSpan textSpan, { Key key, TextStyle style, TextAlign textAlign, TextDirection textDirection, Locale locale, bool softWrap, TextOverflow overflow, double textScaleFactor, int maxLines, String semanticsLabel })
使用构造方法2,对上述属性进行运用,示例如下:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Paint backgroundPaint = new Paint();
backgroundPaint.color = new Color(0xFFFFFFFF);
List shadowList = new List();
shadowList.add(new Shadow(
//阴影颜色
color: new Color(0xFFFF0000),
//模糊程度
blurRadius: 2.0));
return MaterialApp(
debugShowCheckedModeBanner: false,
title: ‘Text widget’,
home: Scaffold(
appBar: new AppBar(
title: new Text(‘Text widget’),
),
body: new Text.rich(
//富文本文本框构造方法,可以显示多种样式的text,第一个参数为 TextSpan
new TextSpan(
text: “我是一段测试文本”,
//文字样式,如果后面的子 TextSpan 没有覆盖该 TextStyle 中的属性,则使用该 TextStyle 指定的样式
style: new TextStyle(
//文字颜色,整体的文字颜色,如果后面的子 TextSpan 没有覆盖该属性,则使用该文字颜色
color: new Color(0xFF000000),
fontSize: 20.0,
decoration: TextDecoration.underline),
//子TextSpan,可以指定多个
children: [
new TextSpan(
text: “Hello”,
style: new TextStyle(
color: new Color(0xFFFF0000),
fontSize: 30.0,
fontStyle: FontStyle.italic)),
new TextSpan(
text: “World”,
style: new TextStyle(
color: new Color(0xFFFFFF00),
fontSize: 40.0,
fontWeight: FontWeight.bold)),
]),
textDirection: TextDirection.ltr,
),
),
);
}
}
运行结果如下:
可以看到我们给Text设置的各种属性基本都呈现出来了
Container是一种常用的控件,由负责布局、绘画、定位和大小调整的几个控件组成,具体来说,Container是由LimitedBox、ConstrainedBox、 Align、Padding、DecoratedBox和Transform控件组成,而不是将Container子类化来产生自定义效果。
| 属性 | 说明 |
| — | :-: |
| padding | 内边距 |
| margin | 外边距 |
| decoration | 装饰 BoxDecoration类型 |
| width | 容器宽 |
| height | 容器高 |
| constraints | 应用于子元素的附加约束 |
| child | 容器子元素 |
| color | 背景色 |
| topCenter | 顶部居中对齐 |
| topLeft | 顶部左对齐 |
| topRight | 顶部右对齐 |
| center | 水平垂直居中对齐 |
| centerLeft | 垂直居中水平居左对齐 |
| centerRight | 垂直居中水平居右对齐 |
| bottomCenter | 底部居中对齐 |
| bottomLeft | 底部居左对齐 |
| bottomRight | 底部居右对齐 |
对上述属性进行运用,示例如下:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var imgUrl =
“https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545122324077&di=1b5986ad937c81c41f4c135ea1a9a026&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fzhidao%2Fwh%253D450%252C600%2Fsign%3Df719f9458f01a18bf0be1a4bab1f2b3e%2Fc2fdfc039245d688a8f2cdcdacc27d1ed31b24e2.jpg”;
return MaterialApp(
debugShowCheckedModeBanner: false,
title: “Container”,
home: Scaffold(
appBar: new AppBar(
title: new Text(“Container”),
),
body: new Container(
//外边距,值为一个 EdgeInsets 对象
margin: EdgeInsets.all(30.0),
//内间距,值为一个 EdgeInsets 对象
padding: EdgeInsets.fromLTRB(30.0, 0, 0, 0),
//Alignment.topLeft:垂直靠顶部水平靠左对齐。
//Alignment.topCenter:垂直靠顶部水平居中对齐。
//Alignment.topRight:垂直靠顶部水平靠右对齐。
//Alignment.centerLeft:垂直居中水平靠左对齐。
//Alignment.center:垂直和水平居中都对齐。
最后
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长。而不成体系的学习效果低效漫长且无助。时间久了,付出巨大的时间成本和努力,没有看到应有的效果,会气馁是再正常不过的。
所以学习一定要找到最适合自己的方式,有一个思路方法,不然不止浪费时间,更可能把未来发展都一起耽误了。
如果你是卡在缺少学习资源的瓶颈上,那么刚刚好我能帮到你。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!