命令式
簡(jiǎn)單講就是需要開(kāi)發(fā)用代碼一步一步進(jìn)行布局,這個(gè)過(guò)程需要開(kāi)發(fā)全程參與。
開(kāi)發(fā)前請(qǐng)熟悉鴻蒙開(kāi)發(fā)指導(dǎo)文檔:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
- Objective-C
ObjectiveC
復(fù)制代碼
UIView *cardView = [[UIView alloc] init];
cardView.backgroundColor = [UIColor whiteColor];
cardView.layer.cornerRadius = 16;
cardView.clipsToBounds = YES;
[self.view addSubview:cardView];
[cardView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(16);
make.right.mas_offset(-16);
make.height.mas_equalTo(116);
make.top.mas_equalTo(100);
}];
NSString *imgUrl = @"https://ke-image.ljcdn.com//110000-inspection//pc1_nBllrJgGj_1.jpg.280x210.jpg";
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor lightGrayColor];
[imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
[cardView addSubview:imgView];
[imgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.mas_offset(0);
make.left.mas_equalTo(0);
make.width.mas_equalTo(107);
}];
UILabel *titleLbl = [[UILabel alloc] init];
titleLbl.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
titleLbl.textColor = [UIColor blackColor];
titleLbl.text = @"萬(wàn)柳書(shū)院新一區(qū) 南北向滿五唯一";
[cardView addSubview:titleLbl];
[titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(imgView.mas_right).mas_offset(12);
make.right.mas_offset(-12);
make.top.mas_equalTo(16);
}];
UILabel *subTitleLbl = [[UILabel alloc] init];
subTitleLbl.textColor = [UIColor blackColor];
subTitleLbl.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
subTitleLbl.text = @"4室2廳/278.35㎡/南 北/萬(wàn)柳書(shū)院";
[cardView addSubview:subTitleLbl];
[subTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(titleLbl);
make.top.mas_equalTo(titleLbl.mas_bottom).mas_offset(8);
}];
UILabel *priceLbl = [[UILabel alloc] init];
priceLbl.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
priceLbl.textColor = [UIColor redColor];
priceLbl.text = @"4238萬(wàn)";
[cardView addSubview:priceLbl];
[priceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(titleLbl);
make.bottom.mas_offset(-16);
}];
UILabel *avgPriceLbl = [[UILabel alloc] init];
avgPriceLbl.textColor = [UIColor lightGrayColor];
avgPriceLbl.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
avgPriceLbl.text = @"155,445元/平";
[cardView addSubview:avgPriceLbl];
[avgPriceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(priceLbl.mas_right).mas_offset(2);
make.right.mas_lessThanOrEqualTo(titleLbl.mas_right);
make.bottom.mas_equalTo(priceLbl);
}];
聲明式
聲明式則是由開(kāi)發(fā)使用語(yǔ)言描述UI頁(yè)面長(zhǎng)什么樣子,之后全權(quán)交給引擎去做
- 對(duì)頁(yè)面結(jié)構(gòu)進(jìn)行大的拆解。比如上面卡片分左右兩大部分
- 選用合適的容器組件進(jìn)行頁(yè)面描述
- 針對(duì)拆解出來(lái)的每個(gè)部分重復(fù)上面的兩步,直到無(wú)法拆解只能使用基本組件描述為止
比如上面的卡片可以進(jìn)行如下的拆分
- 整體是一個(gè)Row容器,分為左右兩大部分,左邊是圖片,右邊是一個(gè)Column容器
- 右邊Column容器又拆分為兩大部分,上面是標(biāo)題和描述,下面是價(jià)格。兩部分按照space-between布局
- 上面的標(biāo)題和描述作為一個(gè)整體,里面拆分成Column的兩個(gè)組件
- 下面價(jià)格可以直接使用系統(tǒng)組件Text
ReactNative
TypeScript
復(fù)制代碼
View
style={{
borderRadius: 8,
marginHorizontal: 16,
flexDirection: 'row',
backgroundColor: 'white',
overflow: 'hidden',
height: 116,
}} >
Image
source={{
uri: 'https://ke-image.ljcdn.com//110000-inspection//pc1_nBllrJgGj_1.jpg.280x210.jpg',
}}
style={{width: 107, backgroundColor: '#eee'}}
/ >
View
style={{
marginVertical: 16,
marginHorizontal: 12,
flex: 1,
justifyContent: 'space-between',
}} >
View >
Text style={{fontSize: 14, color: '#222', fontWeight: '500'}} >
萬(wàn)柳書(shū)院新一區(qū) 南北向滿五唯一
/Text >
Text style={{fontSize: 11, color: '#222', marginTop: 8}} >
4室2廳/278.35㎡/南 北/萬(wàn)柳書(shū)院
/Text >
/View >
View
style={{flexDirection: 'row', marginTop: 8, alignItems: 'flex-end'}} >
Text
style={{
fontSize: 17,
color: '#E62222',
fontWeight: 'bold',
}} >
4238萬(wàn)
/Text >
Text style={{fontSize: 11, color: '#999', marginLeft: 6}} >
155,445元/平
/Text >
/View >
/View >
/View >
Flutter
flutter
復(fù)制代碼
SwiftUI
swift
復(fù)制代碼
HStack(spacing:0) {
AsyncImage(url: URL(string: "https://ke-image.ljcdn.com//110000-inspection//pc1_nBllrJgGj_1.jpg.280x210.jpg"))
.frame(width:107)
.aspectRatio(contentMode: .fill)
.clipped()
VStack(alignment: .leading,
spacing:0) {
VStack(alignment: .leading,
spacing:0) {
Text("萬(wàn)柳書(shū)院新一區(qū) 南北向滿五唯一")
.lineLimit(1)
.font(.system(size: 14))
.foregroundColor(.black)
.fontWeight(.bold)
Text("4室2廳/278.35㎡/南 北/萬(wàn)柳書(shū)院")
.lineLimit(1)
.font(.system(size: 12))
.foregroundColor(.black)
.padding(.top, 8)
}
Spacer()
HStack(alignment: .bottom,
spacing:2) {
Text("4238萬(wàn)")
.font(.system(size: 14))
.foregroundColor(.red)
.fontWeight(.bold)
Text("155,445元/平")
.font(.system(size: 12))
.foregroundColor(.secondary)
.padding(.leading, 2)
}
}
.padding(.vertical, 16)
.padding(.horizontal, 12)
Spacer()
}
.frame(height: 116)
.background(.white)
.clipShape(RoundedRectangle(cornerRadius: 8))
.padding(.horizontal, 16)
}
ArkUI
typescript
復(fù)制代碼
Row() {
Row() {
Image("https://ke-image.ljcdn.com//110000-inspection//pc1_nBllrJgGj_1.jpg.280x210.jpg")
.width(107)
.height("100%")
.objectFit(ImageFit.Cover)
Column() {
Column() {
Text("柳書(shū)院新一區(qū) 南北向滿五唯一")
.fontSize(16)
.fontColor("#222")
.maxLines(1)
Text("4室2廳/278.35㎡/南 北/萬(wàn)柳書(shū)院")
.fontSize(14)
.fontColor("#222")
.maxLines(1)
.margin({ top: 8 })
}
.alignItems(HorizontalAlign.Start)
Row() {
Text("4238萬(wàn)")
.fontSize(15)
.fontColor("#E62222")
.fontWeight(FontWeight.Bold)
Text("155,445元/平")
.fontSize(13)
.fontColor("#222")
.margin({ left: 2 })
}
.justifyContent(FlexAlign.Start)
.alignItems(VerticalAlign.Bottom)
}
.width("100%")
.height("100%")
.padding({ top: 16, bottom: 16, left: 12, right: 12 })
.alignItems(HorizontalAlign.Start)
.justifyContent(FlexAlign.SpaceBetween)
}
.borderRadius(8)
.margin({ left: 16, right: 16 })
.backgroundColor(Color.White)
.justifyContent(FlexAlign.Start)
.clip(true)
}
.height(116)
.width("100%")
`HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`
小結(jié)
- 從上面的例子可以看出來(lái),聲明式語(yǔ)法只需要我們描述UI長(zhǎng)什么樣就行。不需要做太多布局計(jì)算的工作,讓我們少掉一些頭發(fā)
- ArkUI和SwiftUI的語(yǔ)法最像,甚至它們的狀態(tài)管理也很像,都是提供了狀態(tài)綁定和監(jiān)聽(tīng)機(jī)制來(lái)更新UI樣式
審核編輯 黃宇
-
開(kāi)發(fā)
+關(guān)注
關(guān)注
0文章
370瀏覽量
40884 -
代碼
+關(guān)注
關(guān)注
30文章
4816瀏覽量
68873 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2386瀏覽量
42961
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論