|
kfq2bfvyfhd6406098545.gif (60.41 KB, 下載次數(shù): 0)
下載附件
保存到相冊
kfq2bfvyfhd6406098545.gif
2024-9-8 09:53 上傳
9 \# H4 p! K; H. w點擊上方藍色字體,關注我們
" G0 i# o, ?+ y7 \1 E3 p2 O5 |Tablelayout使用表格的方式劃分子組件。: T0 q4 G$ X, o7 i
& b% X5 R. ?! q% n0 o
fxtq3ssw0zm6406098645.png (8.27 KB, 下載次數(shù): 0)
下載附件
保存到相冊
fxtq3ssw0zm6406098645.png
2024-9-8 09:53 上傳
" G* L9 X6 b0 ?* q* @0 {- C. c
( t- {' `. {/ E Y9 o& f2 w3 h; f
TableLayout的共有XML屬性繼承自:Component。% `3 C; T1 B/ t
: L ^0 d' i+ u0 P
TableLayout的自有XML屬性見下表:
% o3 x/ F4 O3 ^7 r7 ]* W6 G5 U( o. |. O4 U' I- W; i; l; d% r W0 ~
. I% w/ W6 S8 W, }6 [4 g' R6 B t* G 2 L& P* |+ J! o
屬性名稱, J1 G4 @; H1 R9 B3 S* c
q; F" B7 {) E
5 E4 X- z! |* _ 中文描述9 W. C8 M9 c# ]( r6 ?9 M
8 W8 N0 n) h, d2 b! g+ y! q
3 F3 N- c1 H0 T 取值
1 D! }' d4 j% [0 I w+ ^4 j+ I
) n9 F4 x% T; l: J) `$ L5 v1 y+ @! L + ~& E( D, H* b5 |7 T' E. g j
取值說明' ]2 ~5 n% H5 P) s1 X1 t7 }
: B4 X1 ^3 t; _( v" ~ l! M9 X1 x # G$ K* \( I* B9 O" r2 F
使用案例& c% o; ^1 b8 ^7 T. `
7 p" _) N& j) y) Y. J" ]
( i5 o* {. P: }1 y* g/ P 2 \5 i+ B8 K/ v, H7 ~
" C; \, g. Q R+ q6 ? 7 u5 h8 Q# V( q8 [
2 _* B5 I& f6 \* Y. i alignment_type8 q7 E" n9 a" m; y
M" h$ u! h# n6 V0 ^2 f9 z
. f7 f0 X$ V3 z% q3 \9 G1 c! r7 j
對齊方式) S [& \0 B7 O) h, u, `
& t1 h% M2 t+ A' Z2 `* p
$ g2 C+ ^+ r6 N; ~4 @6 \ align_edges
8 `0 ?5 ^- k, N' v# x
1 P4 i4 z o. J+ m# H4 j4 Y/ J, O; g
% h- D' G' W! e 表示TableLayout內的組件按邊界對齊。3 Q4 D) h7 I- S' F9 @/ k; l
/ y6 h8 R' p _
4 d, j- n' C7 R" d0 c0 H
ohos:alignment_type="align_edges"* P' {% ^. P. Z5 }& Z
/ G2 o+ M& l- a
8 P. Q1 B$ y: w+ z; M* u# X( T! p# K
' x: p6 _) P; p9 V
; x" P7 O2 S( U& m align_contents
@6 d) F( _9 c. F# D
' y z7 _8 m3 b4 H; Y
9 B0 c. { i+ @; A; g& I* N1 t 表示TableLayout內的組件按邊距對齊。9 U; n% ]( x4 j
) |: z6 G8 F6 L# p, T: ^
+ r% H! A4 q8 t ohos:alignment_type="align_contents"
# L' o# S. ^, p " u6 I0 J* g/ J9 c3 S: s, i7 @( T. v: _ Z
; `: i, U, s" ]% ]# b' ]3 U6 n! ~
6 N' d* k' o$ U) S9 Y
1 f/ v( E" Z2 B& e* F0 q& ~0 p$ P
column_count
6 u7 S% i K9 F* |" p " v& f3 w8 }" c! P+ h- ~
( R( n# g1 a& E- B
列數(shù), h% N9 n' M$ G! D. g
5 R3 x6 ?+ V4 C5 Y/ T
7 p( ?& L& Q/ r, w- Z' p/ j integer類型
6 ~5 [ x* K, J8 V 4 d0 O& ~6 }' \, o! C/ o
4 J! Y) q; _( M) r( `
可以直接設置整型數(shù)值,也可以引用integer資源。) \8 Y8 i/ M5 x! y
u6 J ~6 V+ r* a ) M; D# H) h; v, i; d; B
ohos:column_count="3"# }0 V4 o6 I( ^0 J/ D, }
# n* g& w# |: N5 D7 [ ohos:column_count="$integer:count"4 A4 h' F/ L! p
4 x( i. s! i- M! E
( y2 Z3 ]' I4 p! k
9 a' N( h2 V+ C0 m5 A * H/ ?" N5 {! T$ h
row_count
- S# N$ B3 h5 L& ~& d( Q5 J5 ^ " l0 r9 n) {: w+ g/ D; y2 u+ G# q7 R
0 N& a3 c+ Z6 N( n. ? 行數(shù)# [) M8 c8 g: o1 ?+ @( h
+ y0 n6 A/ B: i8 }' s+ y* ?, K0 G
1 S, z, F; s6 z6 y& v1 _# u& D integer類型
! C2 \* R) V9 y& q
* `; t* O* r. p- w 5 g9 f2 n: e$ b5 @6 Y
可以直接設置整型數(shù)值,也可以引用integer資源。/ A2 ] X! k4 m) n4 W
; s' d) e$ U( f4 y# ]# J1 ?
6 R4 M' X" x b" t N1 z7 Y0 a
ohos:row_count="2"5 E/ i; W1 h' W) p
8 r2 w U1 M Y E% R5 ]: e ohos:row_count="$integer:count"
6 a$ Q! q# [% m# W& U
3 c! S; a! ?% P* m" H5 x , k6 w, ~! B$ p- `. b3 p2 ?- R
* b& l$ | I; q; b8 \
2 D& i; R' V8 e0 s3 q3 K. t
orientation4 S9 W: U" y2 \% F
6 \ q, z/ s! p4 K3 r2 h( N. c! R
! k6 n+ s, }0 } s5 c: r) B
排列方向9 }: f$ \% ]. L0 X
6 d0 v# o" d5 D% B: v& S9 V , y2 N7 j p# ]
horizontal
/ R3 J9 ^4 I9 I9 h
+ o& `* ?' }4 y
4 l+ {9 w8 z4 o: `5 ]5 K 表示水平方向布局。; N! Z9 e! z5 M c3 b% h
* r0 U1 B( O. s8 m" t* x& v
9 z1 }. k' L( i3 n+ n
ohos:orientation="horizontal"
- ?+ Z) a; K! [ A' m! X0 y7 {5 Y9 n 8 G( `; F- A7 {! A! A
/ o+ h% ?# ?5 w$ B
( q4 b7 q' Z* M
g/ X# L& n5 C' |! |, \ vertical. }4 I2 @( g2 B% [
- a! v H3 D: y% ~ N U/ k1 M7 M4 L% U$ d, V0 d& }
表示垂直方向布局。
& {; U9 \- d4 r6 ]+ b5 H6 I 0 P0 Y5 n, c, e( ^5 b( b, ~. _2 |
0 f( E/ A2 @5 d! ?. \* y3 |7 h
ohos:orientation="vertical"( f# O) v" v# T/ |4 H8 `
3 f- R) O: R- G! b
6 q+ i- S$ Z& W/ L. C在XML中創(chuàng)建TableLayout,示例代碼如下:
@- ]; A" e5 e/ e% P$ m$ x; B[table]/tablelayoutxmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:background_element="#87CEEB" ohos:padding="[table]/tablelayout
4 l" M# C+ G( n8 d6 |. {7 o在graphic文件夾下創(chuàng)建Text的背景table_text_bg_element.xml,示例代碼如下:. P& k* u0 m1 M0 [
"http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"> ohos:radius="5vp"/> ohos:width="1vp" ohos:color="gray"/> ohos:color="#00BFFF"/>"http://schemas.huawei.com/res/ohos" # @, Z5 C- U K: [; z
在TableLayout布局中添加子組件。
9 x- z1 ?/ _; y' B( |7 O, l6 H4 Z5 b
xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:background_element="#87CEEB" ohos:padding="8vp"> ohos:height="60vp" ohos:width="60vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="8vp" ohos:text="1" ohos:text_alignment="center" ohos:text_size="20fp"/>0 T2 S: | I% g5 ~ f' O" P+ ^( c
ohos:height="60vp" ohos:width="60vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="8vp" ohos:text="2" ohos:text_alignment="center" ohos:text_size="20fp"/>" Q' [ c- O# T% z
ohos:height="60vp" ohos:width="60vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="8vp" ohos:text="3" ohos:text_alignment="center" ohos:text_size="20fp"/>
6 N" s4 p3 s8 {. U0 U3 x ohos:height="60vp" ohos:width="60vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="8vp" ohos:text="4" ohos:text_alignment="center" ohos:text_size="20fp"/>[table]
' Z3 r9 w0 F( D, ^( W& l: g/ l6 UTableLayout默認一列多行。
5 d- D6 M i9 n) M7 B0 `6 y
% I( @4 ^ a3 G
dy10atfv2fc6406098745.png (6.88 KB, 下載次數(shù): 0)
下載附件
保存到相冊
dy10atfv2fc6406098745.png
2024-9-8 09:53 上傳
, L' u. K5 K" F |+ F9 @- m5 N
* `9 C2 x4 j# T2 T$ m! L: Y
設置行列數(shù):
, L. _$ C7 ?1 ]/ g$ o: k) d! Y9 i/ G$ G4 r& C
... ohos:row_count="2" ohos:column_count="2">
: y$ G4 Y! M: N! H! F/ |9 z設置TableLayout的行為2,列為2效果。. y, Y' W: v/ G7 j+ F% H D
9 R6 M# T, K! M+ g# B# \1 O
flwcwvjr20g6406098845.png (4.49 KB, 下載次數(shù): 0)
下載附件
保存到相冊
flwcwvjr20g6406098845.png
2024-9-8 09:53 上傳
. d# w; y: _6 w1 L
* C" g( ?! \0 ~1 F6 J* b
在XML中設置布局排列方向,以“vertical”為例:$ I8 B) u* S1 J9 @2 L
0 p$ l; j6 X% M; P7 p
... ohos:orientation="vertical"> ...
$ j" B( J) U7 z/ I- @0 ^設置布局排列方向為“vertical”的效果。; V1 ]1 ?4 i; U1 L1 U! g
( w0 t1 u n5 A2 \! w$ _
esbsvmwndzi6406098945.png (4.53 KB, 下載次數(shù): 0)
下載附件
保存到相冊
esbsvmwndzi6406098945.png
2024-9-8 09:53 上傳
0 J/ i e8 {# `6 G& u1 z3 t1 @0 U0 V
TableLayout提供兩種對齊方式,邊距對齊“align_contents”、邊界對齊“align_edges”,默認為邊距對齊“align_contents”。代碼如下:
8 X. S, m* G9 ~" x6 }! {* }7 ]3 G
0 j! d; i5 j( S0 `+ Q$ h* v5 Hxmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_content" ohos:width="match_content" ohos:alignment_type="align_contents" ohos:background_element="$graphic:layout_borderline" ohos:column_count="3" ohos:padding="8vp">
- c3 h. T$ L% {( b" F5 ~3 y. T ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="8vp" ohos:padding="8vp" ohos:text="1" ohos:text_alignment="center" ohos:text_size="14fp"/>1 w6 s5 `, w% a2 j( k
ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="16vp" ohos:padding="8vp" ohos:text="2" ohos:text_alignment="center" ohos:text_size="14fp"/>
6 \9 K( T- c' G) \1 o" m ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="32vp" ohos:padding="8vp" ohos:text="3" ohos:text_alignment="center" ohos:text_size="14fp"/>
. r" m- f- g& _ w ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="32vp" ohos:padding="8vp" ohos:text="4" ohos:text_alignment="center" ohos:text_size="14fp"/>
; K* b9 f: M, p2 R ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="16vp" ohos:padding="8vp" ohos:text="5" ohos:text_alignment="center" ohos:text_size="14fp"/>
5 G3 ^ E; |0 v, O3 i+ E ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="8vp" ohos:padding="8vp" ohos:text="6" ohos:text_alignment="center" ohos:text_size="14fp"/>1 L( W w% _8 F& j3 T3 [) d" y
邊距對齊效果:- \1 I8 N* m/ b: X! H4 q
1 i8 g1 o$ A% K! d1 |' o5 p, ~8 s, F
c51f3orwvkx6406099046.png (5.83 KB, 下載次數(shù): 1)
下載附件
保存到相冊
c51f3orwvkx6406099046.png
2024-9-8 09:53 上傳
: ~0 m; d! F5 ^5 P# J# u; _) R/ f) z; H7 X# W2 L8 w
將TableLayout的對齊方式修改為邊界對齊。
0 Q) D; }, e0 ^& ], D9 \& R0 \. Z* f4 N# o) t# q: e, w& L X
... ohos:alignment_type="align_edges"> ... U3 e- j( y% y0 R
邊界對齊效果:
) A) ] U% C& e7 F
; Y4 \4 A" O/ a+ T2 I* |
pqnnz22v2co6406099146.png (5.86 KB, 下載次數(shù): 0)
下載附件
保存到相冊
pqnnz22v2co6406099146.png
2024-9-8 09:53 上傳
/ R3 |1 {/ K8 Z" Z2 x. `5 p; P+ m5 C% z. W3 H2 A
引用graphic文件夾下的背景資源文件為layout_borderline.xml,示例代碼如下:- Y8 V! ^6 I# w: [' @2 W% f
) s; p* \9 F0 e) Z5 e! k- r
"http://schemas.huawei.com/res/ohos" ohos:shape="rectangle"> ohos:radius="5vp"/> ohos:width="1vp" ohos:color="gray"/>
$ [( c1 n7 x& a& Y( g/ ]
& R" o# ?% Y: v$ I1 l$ S" tTableLayout合并單元格的效果可以通過設置子組件的行列屬性來實現(xiàn)。
$ x6 Z9 d, K Q1 Z8 j2 ]4 L1 a; m. r) Q8 r
設置子組件的行列屬性均為2的效果展示:
5 |' ^/ H- Y% G+ @8 o) V2 ]1 M* T& w2 |0 a
ek4edwbof416406099246.png (5.79 KB, 下載次數(shù): 0)
下載附件
保存到相冊
ek4edwbof416406099246.png
2024-9-8 09:53 上傳
- M, a5 e! O, m* k' U/ W0 d, D1 U3 Q. S6 [3 t- |. }$ C
在XML中創(chuàng)建TableLayout,并添加子組件,代碼如下:- B) ?# m; E9 l
9 s0 N, V; w; r. D' f4 ?
xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_content" ohos:width="match_content" ohos:alignment_type="align_edges" ohos:background_element="$graphic:layout_borderline" ohos:column_count="3" ohos:padding="8vp" ohos:row_count="3">
) X( m- P( C7 A2 b6 A: ]. u1 F ohos:id="$+id:text_one" ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="16vp" ohos:padding="8vp" ohos:text="1" ohos:text_alignment="center" ohos:text_size="14fp"/>/ f& G! y/ b/ ~: l* c c2 i( k9 E
ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="16vp" ohos:padding="8vp" ohos:text="2" ohos:text_alignment="center" ohos:text_size="14fp"/>
$ C1 F9 e" |' b3 ~4 J: b ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="16vp" ohos:padding="8vp" ohos:text="3" ohos:text_alignment="center" ohos:text_size="14fp"/>
+ a( l2 y7 B5 p$ G' `, B5 H ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="16vp" ohos:padding="8vp" ohos:text="4" ohos:text_alignment="center" ohos:text_size="14fp"/>
3 Q6 {- R" l& L8 T) q* } ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="16vp" ohos:padding="8vp" ohos:text="5" ohos:text_alignment="center" ohos:text_size="14fp"/>
- V% b3 l0 e0 R, K# z; Z ohos:height="48vp" ohos:width="48vp" ohos:background_element="$graphic:table_text_bg_element" ohos:margin="16vp" ohos:padding="8vp" ohos:text="6" ohos:text_alignment="center" ohos:text_size="14fp"/>. ]4 W# x3 P$ w
在Java代碼中設置子組件的行列屬性,代碼如下:% {- a& ~' F5 e) I( G* H# b
4 T; a0 } ]; \1 d
@Override protected void onStart(Intent intent) { ... Component component = findComponentById(ResourceTable.Id_text_one); TableLayout.LayoutConfig tlc = new TableLayout.LayoutConfig(vp2px(72), vp2px(72)); tlc.columnSpec = TableLayout.specification(TableLayout.DEFAULT, 2); tlc.rowSpec = TableLayout.specification(TableLayout.DEFAULT, 2); component.setLayoutConfig(tlc); }
# w6 [6 e! a! i- m private int vp2px(float vp) { return AttrHelper.vp2px(vp, getContext()); }
y2 Z5 ? V5 Z7 q% ?3 j- {8 }" F在設置子組件的行列屬性時,TableLayout剩余的行數(shù)和列數(shù)必須大于等于該子組件所設置的行數(shù)和列數(shù)。
4 [1 J5 H6 R9 w4 c% i% o; X r: ^+ Z. p
目前僅支持Java代碼設置TableLayout子組件的行列屬性。5 C; _1 r: Q/ B9 J/ c
在創(chuàng)建子組件的行列屬性時,還可設置子組件的對齊方式,修改上述Java代碼如下:5 }. _/ A/ P; p L# {6 @' o3 f5 R
* W9 N' k0 E4 u@Override protected void onStart(Intent intent) { ... tlc.columnSpec = TableLayout.specification(TableLayout.DEFAULT, 2, TableLayout.Alignment.ALIGNMENT_FILL); tlc.rowSpec = TableLayout.specification(TableLayout.DEFAULT, 2, TableLayout.Alignment.ALIGNMENT_FILL); ... }
5 W# W, j4 v7 f# G子組件的對齊方式設置為ALIGNMENT_FILL的效果:
/ R) M6 N' }* q: J3 s7 X; N+ f
8 b% x3 t, `. z! ]6 h8 ^
dwv3shrkjvq6406099346.png (5.6 KB, 下載次數(shù): 0)
下載附件
保存到相冊
dwv3shrkjvq6406099346.png
2024-9-8 09:53 上傳
; f" Y' v6 h% i- g' P
; g( d7 F" B7 z( ~
設置子組件的權重,代碼如下:
( M5 K+ B' [7 A; |
" [# `) X' K: R4 P@Override protected void onStart(Intent intent) { ... TableLayout.LayoutConfig tlc = new TableLayout.LayoutConfig(0, vp2px(48)); tlc.columnSpec = TableLayout.specification(TableLayout.DEFAULT, 1, 1.0f); tlc.rowSpec = TableLayout.specification(TableLayout.DEFAULT, 1); ]4 `) Y+ o& T0 C3 T) C% c
findComponentById(ResourceTable.Id_text_one).setLayoutConfig(tlc); findComponentById(ResourceTable.Id_text_two).setLayoutConfig(tlc); findComponentById(ResourceTable.Id_text_three).setLayoutConfig(tlc); findComponentById(ResourceTable.Id_text_four).setLayoutConfig(tlc); findComponentById(ResourceTable.Id_text_five).setLayoutConfig(tlc); findComponentById(ResourceTable.Id_text_six).setLayoutConfig(tlc); }
$ ^4 U8 b4 {% r, J3 O8 P) K上述代碼將子組件的寬度權重設置為1.0,每行子組件會均分TableLayout的寬度,所以需要設置TableLayout為固定寬度或match_parent。& f, f0 X4 T5 Q/ H2 p
9 o: p1 r& S" w$ D8 O
ohos:width="match_parent" ...>
5 `( N/ C' o6 }/ T% |+ Y) _ ohos:id="$+id:text_one" .../>9 C' o1 c! }( v: D
ohos:id="$+id:text_two" .../>
/ f+ y( Y% A$ _/ s ohos:id="$+id:text_three" .../>
0 \7 Y& H; K: o1 R3 k ohos:id="$+id:text_four" .../>
& j6 @2 A- ] _ ohos:id="$+id:text_five" .../>
, |8 X, ?- `% m" W* f; g1 e ohos:id="$+id:text_six" .../>[table]
2 q9 W# c* Z! _. @將子組件的寬度權重設置為1.0的效果展示: ]& M; x; E4 S, ^5 P" N) V: \
" W+ M+ r1 \ [" [& O$ P& D
zxuaeir1fhl6406099446.png (5.58 KB, 下載次數(shù): 0)
下載附件
保存到相冊
zxuaeir1fhl6406099446.png
2024-9-8 09:53 上傳
5 V) Y/ T4 }) |$ t. @9 r! j d9 I# E
7 y6 A+ }! m7 F
mdpiy0r3oy16406099546.png (401 Bytes, 下載次數(shù): 0)
下載附件
保存到相冊
mdpiy0r3oy16406099546.png
2024-9-8 09:53 上傳
! b8 e- ?0 j( L7 l) B+ e, u
往期推薦基于Mobile SDK V5版固件開發(fā)大疆無人機手機端遙控器(5)" T9 T1 o1 E9 _' D- B1 U! f/ ?4 `
一種高速ADC和DAC轉換電路分享
]. }- T5 p' _0 W! d3 G一文搞懂CAN和CAN FD總線協(xié)議
O: |6 m8 k$ q- p5 f( cLabVIEW和Arduino的巧妙結合(基礎篇—1)9 e. i# {( `) q& n8 c) A
LabVIEW設計自定義滾動條
6 d7 l) }, v( ~; _. C" k* p, \$ q/ K( o$ e8 s* o( h
qwg2v3bbf426406099646.jpg (71.14 KB, 下載次數(shù): 0)
下載附件
保存到相冊
qwg2v3bbf426406099646.jpg
2024-9-8 09:53 上傳
3 }# F% Y# X" U" M3 x2 h- D- T
/ u8 _6 }; z( [
uaetuwcvdjn6406099746.gif (45.46 KB, 下載次數(shù): 0)
下載附件
保存到相冊
uaetuwcvdjn6406099746.gif
2024-9-8 09:53 上傳
' b9 _; g% u c! U% y點擊閱讀原文,更精彩~ |
|