React Native之盒子模型BoxApp

盒子模型BoxApp

HTML5实现盒子模型显示

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <metacharset="utf-8">
  5. <title></title>
  6. <style>
  7. .height50 {
  8. height:50px;
  9. }
  10. .height400 {
  11. height:400px;
  12. }
  13. .height300 {
  14. height:300px;
  15. }
  16. .height200 {
  17. height:200px;
  18. }
  19. .height100 {
  20. height:100px;
  21. }
  22. .width400 {
  23. width:400px;
  24. }
  25. .bgred {
  26. background-color:#6AC5AC;
  27. }
  28. .bggreen {
  29. background-color:#414142;
  30. }
  31. .bgyellow {
  32. background-color:#D64078;
  33. }
  34. .box {
  35. display: flex;
  36. flex-direction: column;
  37. flex:1;
  38. position: relative;
  39. color:#FDC72F;
  40. line-height:1em;
  41. }
  42. .label {
  43. top:0;
  44. left:0;
  45. padding:03px3px0;
  46. position: absolute;
  47. background-color:#FDC72F;
  48. color: white;
  49. line-height:1em;
  50. }
  51. .top {
  52. width:100%;
  53. justify-content: center;
  54. display: flex;
  55. align-items: center;
  56. }
  57. .bottom {
  58. width:100%;
  59. display: flex;
  60. justify-content: center;
  61. align-items: center;
  62. }
  63. .right {
  64. width:50px;
  65. display: flex;
  66. justify-content: space-around;
  67. align-items: center;
  68. }
  69. .left {
  70. width:50px;
  71. display: flex;
  72. justify-content: space-around;
  73. align-items: center;
  74. }
  75. .heightdashed {
  76. position: absolute;
  77. right:20px;
  78. height:100%;
  79. border-left:1px solid #FDC72F;
  80. }
  81. .widthdashed {
  82. position: absolute;
  83. left:0px;
  84. width:100%;
  85. bottom:24px;
  86. border-top:1px solid #FDC72F;
  87. }
  88. .margginBox {
  89. position:absolute;
  90. top:100px;
  91. padding-left:7px;
  92. padding-right:7px;
  93. }
  94. .borderBox {
  95. flex:1;
  96. display: flex;
  97. justify-content: space-between;
  98. }
  99. .paddingBox {
  100. flex:1;
  101. display: flex;
  102. justify-content: space-between;
  103. }
  104. .elementBox{
  105. flex:1;
  106. display: flex;
  107. justify-content: space-between;
  108. }
  109. .measureBox {
  110. flex:1;
  111. display: flex;
  112. justify-content: flex-end;
  113. align-items: flex-end;
  114. }
  115. </style>
  116. </head>
  117. <body>
  118. <spanclass="margginBox">
  119. <spanclass="box height400 width400">
  120. <spanclass="label">
  121. margin
  122. </span>
  123. <spanclass="top height50 bgred">
  124. top
  125. </span>
  126. <spanclass="borderBox">
  127. <spanclass="left bgred">
  128. left
  129. </span>
  130. <spanclass="box height300 ">
  131. <spanclass="label">
  132. border
  133. </span>
  134. <spanclass="top height50 bggreen">
  135. top
  136. </span>
  137. <spanclass="paddingBox">
  138. <spanclass="left bggreen">
  139. left
  140. </span>
  141. <spanclass="box height200 ">
  142. <spanclass="label">
  143. padding
  144. </span>
  145. <spanclass="top height50 bgyellow">
  146. top
  147. </span>
  148. <spanclass="elementBox">
  149. <spanclass="left bgyellow">
  150. left
  151. </span>
  152. <spanclass="box height100 ">
  153. <spanclass="label">
  154. element
  155. </span>
  156. <spanclass="widthdashed">
  157. </span>
  158. <spanclass="heightdashed">
  159. </span>
  160. <spanclass="measureBox">
  161. <spanclass="right">
  162. height
  163. </span>
  164. </span>
  165. <spanclass="bottom height50">
  166. width
  167. </span>
  168. </span>
  169. <spanclass="right bgyellow">
  170. right
  171. </span>
  172. </span>
  173. <spanclass="bottom height50 bgyellow">
  174. bottom
  175. </span>
  176. </span>
  177. <spanclass="right bggreen">
  178. right
  179. </span>
  180. </span>
  181. <spanclass="bottom height50 bggreen">
  182. bottom
  183. </span>
  184. </span>
  185. <spanclass="right bgred">
  186. right
  187. </span>
  188. </span>
  189. <spanclass="bottom height50 bgred">
  190. bottom
  191. </span>
  192. </span>
  193. </span>
  194. </body>
  195. </html>

React Native实现盒子模型显示

  • 类似HTML写法
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. */
  5. 'use strict';
  6. importReact,{
  7. AppRegistry,
  8. Component,
  9. StyleSheet,
  10. Text,
  11. View
  12. }from'react-native';
  13. classDongFangextendsComponent{
  14. render(){
  15. return(
  16. <View style={[BoxStyles.margginBox]}ref="lab1">
  17. <View style={[BoxStyles.box,BoxStyles.height400,BoxStyles.width400]}>
  18. <View style={[BoxStyles.top,BoxStyles.height50,BoxStyles.bgred]}>
  19. <Text style={BoxStyles.yellow}>top</Text></View>
  20. <View style={[BoxStyles.borderBox]}>
  21. <View style={[BoxStyles.left,BoxStyles.bgred]}>
  22. <Text style={BoxStyles.yellow}>left</Text></View>
  23. <View style={[BoxStyles.box,BoxStyles.height300]}>
  24. <View style={[BoxStyles.top,BoxStyles.height50,BoxStyles.bggreen]}>
  25. <Text style={BoxStyles.yellow}>top</Text></View>
  26. <View style={[BoxStyles.paddingBox]}>
  27. <View style={[BoxStyles.left,BoxStyles.bggreen]}>
  28. <Text style={BoxStyles.yellow}>left</Text></View>
  29. <View style={[BoxStyles.box,BoxStyles.height200]}>
  30. <View style={[BoxStyles.top,BoxStyles.height50,BoxStyles.bgyellow]}>
  31. <Text style={BoxStyles.yellow}>top</Text></View>
  32. <View style={[BoxStyles.elementBox]}>
  33. <View style={[BoxStyles.left,BoxStyles.bgyellow]}>
  34. <Text style={BoxStyles.yellow}>left</Text></View>
  35. <View style={[BoxStyles.box,BoxStyles.height100]}>
  36. <View style={[BoxStyles.label]}>
  37. <Text style={BoxStyles.white}>element</Text></View>
  38. <View style={[BoxStyles.widthdashed]}></View>
  39. <View style={[BoxStyles.heightdashed]}></View>
  40. <View style={[BoxStyles.measureBox]}>
  41. <View style={[BoxStyles.right]}>
  42. <Text style={[BoxStyles.yellow]}>height</Text></View>
  43. </View>
  44. <View style={[BoxStyles.bottom,BoxStyles.height50]}>
  45. <Text style={BoxStyles.yellow}>width</Text></View>
  46. </View>
  47. <View style={[BoxStyles.right,BoxStyles.bgyellow]}><Text style={BoxStyles.yellow}>right</Text></View>
  48. </View>
  49. <View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles.bgyellow]}>
  50. <Text style={BoxStyles.yellow}>bottom</Text></View>
  51. <View style={[BoxStyles.label]}>
  52. <Text style={BoxStyles.white}>padding</Text></View>
  53. </View>
  54. <View style={[BoxStyles.right,BoxStyles.bggreen]}><Text style={BoxStyles.yellow}>right</Text></View>
  55. </View>
  56. <View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles.bggreen]}>
  57. <Text style={BoxStyles.yellow}>bottom</Text></View>
  58. <View style={[BoxStyles.label]}><Text style={BoxStyles.white}>border</Text></View>
  59. </View>
  60. <View style={[BoxStyles.right,BoxStyles.bgred]}>
  61. <Text style={BoxStyles.yellow}>right</Text></View>
  62. </View>
  63. <View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles.bgred]}>
  64. <Text style={BoxStyles.yellow}>bottom</Text></View>
  65. <View style={[BoxStyles.label]}><Text style={BoxStyles.white}>margin</Text></View>
  66. </View>
  67. </View>
  68. );
  69. }
  70. }
  71. constBoxStyles=StyleSheet.create({
  72. height50:{
  73. height:50,
  74. },
  75. height400:{
  76. height:400,
  77. },
  78. height300:{
  79. height:300,
  80. },
  81. height200:{
  82. height:200,
  83. },
  84. height100:{
  85. height:100,
  86. },
  87. width400:{
  88. width:400,
  89. },
  90. width300:{
  91. width:300,
  92. },
  93. width200:{
  94. width:200,
  95. },
  96. width100:{
  97. width:100,
  98. },
  99. bgred:{
  100. backgroundColor:'#6AC5AC',
  101. },
  102. bggreen:{
  103. backgroundColor:'#414142',
  104. },
  105. bgyellow:{
  106. backgroundColor:'#D64078',
  107. },
  108. box:{
  109. flexDirection:'column',
  110. flex:1,
  111. position:'relative',
  112. },
  113. label:{
  114. top:0,
  115. left:0,
  116. paddingTop:0,
  117. paddingRight:3,
  118. paddingBottom:3,
  119. paddingLeft:0,
  120. position:'absolute',
  121. backgroundColor:'#FDC72F',
  122. },
  123. top:{
  124. justifyContent:'center',
  125. alignItems:'center',
  126. },
  127. bottom:{
  128. justifyContent:'center',
  129. alignItems:'center',
  130. },
  131. right:{
  132. width:50,
  133. justifyContent:'space-around',
  134. alignItems:'center',
  135. },
  136. left:{
  137. width:50,
  138. justifyContent:'space-around',
  139. alignItems:'center',
  140. },
  141. heightdashed:{
  142. bottom:0,
  143. top:0,
  144. right:20,
  145. borderLeftWidth:1,
  146. position:'absolute',
  147. borderLeftColor:'#FDC72F',
  148. },
  149. widthdashed:{
  150. bottom:25,
  151. left:0,
  152. right:0,
  153. borderTopWidth:1,
  154. position:'absolute',
  155. borderTopColor:'#FDC72F',
  156. },
  157. yellow:{
  158. color:'#FDC72F',
  159. fontWeight:'900',
  160. },
  161. white:{
  162. color:'white',
  163. fontWeight:'900',
  164. },
  165. margginBox:{
  166. position:'absolute',
  167. top:100,
  168. paddingLeft:7,
  169. paddingRight:7,
  170. },
  171. borderBox:{
  172. flex:1,
  173. justifyContent:'space-between',
  174. flexDirection:'row',
  175. },
  176. paddingBox:{
  177. flex:1,
  178. justifyContent:'space-between',
  179. flexDirection:'row',
  180. },
  181. elementBox:{
  182. flex:1,
  183. justifyContent:'space-between',
  184. flexDirection:'row',
  185. },
  186. measureBox:{
  187. flex:1,
  188. flexDirection:'row',
  189. justifyContent:'flex-end',
  190. alignItems:'flex-end',
  191. },
  192. container:{
  193. flex:1,
  194. justifyContent:'center',
  195. alignItems:'center',
  196. backgroundColor:'#F5FCFF',
  197. },
  198. welcome:{
  199. fontSize:20,
  200. textAlign:'center',
  201. margin:10,
  202. },
  203. instructions:{
  204. textAlign:'center',
  205. color:'#333333',
  206. marginBottom:5,
  207. },
  208. });
  209. AppRegistry.registerComponent('DongFang',()=>DongFang);
  • ReactJs写法
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. */
  5. 'use strict';
  6. importReact,{
  7. AppRegistry,
  8. Component,
  9. StyleSheet,
  10. Text,
  11. View
  12. }from'react-native';
  13. classBoxextendsComponent{
  14. render(){
  15. return(
  16. <View style={[BoxStyles.box,BoxStyles[this.props.width],BoxStyles[this.props.height]]}>
  17. <View style={[BoxStyles.top,BoxStyles.height50,BoxStyles[this.props.classBg]]}><Text>top</Text></View>
  18. <View style={[BoxStyles[this.props.childName]]}>
  19. <View style={[BoxStyles.left,BoxStyles[this.props.classBg]]}><Text>left</Text></View>
  20. {this.props.children}
  21. <View style={[BoxStyles.right,BoxStyles[this.props.classBg]]}><Text>right</Text></View>
  22. </View>
  23. <View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles[this.props.classBg]]}><Text>bottom</Text></View>
  24. <View style={[BoxStyles.label]}><Text>{this.props.boxName}</Text></View>
  25. </View>
  26. )
  27. }
  28. }
  29. classMargginBoxextendsComponent{
  30. render(){
  31. return(
  32. <View style={[BoxStyles.margginBox]}>
  33. <Box childName="borderBox" height="height400" width="width400" boxName="margin" classBg="bgred">{this.props.children}</Box>
  34. </View>
  35. )
  36. }
  37. }
  38. classBorderBoxextendsComponent{
  39. render(){
  40. return(
  41. <Box childName="paddingBox" height="height300" width="width300" boxName="border" classBg="bggreen">{this.props.children}</Box>
  42. )
  43. }
  44. }
  45. classPaddingBoxextendsComponent{
  46. render(){
  47. return(
  48. <Box childName="elementBox" height="height200" width="width200" boxName="padding" classBg="bgyellow">{this.props.children}</Box>
  49. )
  50. }
  51. }
  52. classElementBoxextendsComponent{
  53. render(){
  54. return(
  55. <View style={[BoxStyles.box,BoxStyles.height100]}>
  56. <View style={[BoxStyles.measureBox]}>
  57. <View style={[BoxStyles.right]}><Text>height</Text></View>
  58. </View>
  59. <View style={[BoxStyles.bottom,BoxStyles.height50]}><Text>width</Text></View>
  60. <View style={[BoxStyles.label]}><Text>element</Text></View>
  61. <View style={[BoxStyles.widthdashed]}></View>
  62. <View style={[BoxStyles.heightdashed]}></View>
  63. </View>
  64. )
  65. }
  66. }
  67. classDongFangextendsComponent{
  68. render(){
  69. return(
  70. <MargginBox>
  71. <BorderBox>
  72. <PaddingBox>
  73. <ElementBox>
  74. </ElementBox>
  75. </PaddingBox>
  76. </BorderBox>
  77. </MargginBox>
  78. )
  79. }
  80. }
  81. constBoxStyles=StyleSheet.create({
  82. height50:{
  83. height:50,
  84. },
  85. height400:{
  86. height:400,
  87. },
  88. height300:{
  89. height:300,
  90. },
  91. height200:{
  92. height:200,
  93. },
  94. height100:{
  95. height:100,
  96. },
  97. width400:{
  98. width:400,
  99. },
  100. width300:{
  101. width:300,
  102. },
  103. width200:{
  104. width:200,
  105. },
  106. width100:{
  107. width:100,
  108. },
  109. bgred:{
  110. backgroundColor:'#6AC5AC',
  111. },
  112. bggreen:{
  113. backgroundColor:'#414142',
  114. },
  115. bgyellow:{
  116. backgroundColor:'#D64078',
  117. },
  118. box:{
  119. flexDirection:'column',
  120. flex:1,
  121. position:'relative',
  122. },
  123. label:{
  124. top:0,
  125. left:0,
  126. paddingTop:0,
  127. paddingRight:3,
  128. paddingBottom:3,
  129. paddingLeft:0,
  130. position:'absolute',
  131. backgroundColor:'#FDC72F',
  132. },
  133. top:{
  134. justifyContent:'center',
  135. alignItems:'center',
  136. },
  137. bottom:{
  138. justifyContent:'center',
  139. alignItems:'center',
  140. },
  141. right:{
  142. width:50,
  143. justifyContent:'space-around',
  144. alignItems:'center',
  145. },
  146. left:{
  147. width:50,
  148. justifyContent:'space-around',
  149. alignItems:'center',
  150. },
  151. heightdashed:{
  152. bottom:0,
  153. top:0,
  154. right:20,
  155. borderLeftWidth:1,
  156. position:'absolute',
  157. borderLeftColor:'#FDC72F',
  158. },
  159. widthdashed:{
  160. bottom:25,
  161. left:0,
  162. right:0,
  163. borderTopWidth:1,
  164. position:'absolute',
  165. borderTopColor:'#FDC72F',
  166. },
  167. yellow:{
  168. color:'#FDC72F',
  169. fontWeight:'900',
  170. },
  171. white:{
  172. color:'white',
  173. fontWeight:'900',
  174. },
  175. margginBox:{
  176. position:'absolute',
  177. top:100,
  178. paddingLeft:7,
  179. paddingRight:7,
  180. },
  181. borderBox:{
  182. flex:1,
  183. justifyContent:'space-between',
  184. flexDirection:'row',
  185. },
  186. paddingBox:{
  187. flex:1,
  188. justifyContent:'space-between',
  189. flexDirection:'row',
  190. },
  191. elementBox:{
  192. flex:1,
  193. justifyContent:'space-between',
  194. flexDirection:'row',
  195. },
  196. measureBox:{
  197. flex:1,
  198. flexDirection:'row',
  199. justifyContent:'flex-end',
  200. alignItems:'flex-end',
  201. },
  202. container:{
  203. flex:1,
  204. justifyContent:'center',
  205. alignItems:'center',
  206. backgroundColor:'#F5FCFF',
  207. },
  208. welcome:{
  209. fontSize:20,
  210. textAlign:'center',
  211. margin:10,
  212. },
  213. instructions:{
  214. textAlign:'center',
  215. color:'#333333',
  216. marginBottom:5,
  217. },
  218. });
  219. AppRegistry.registerComponent('DongFang',()=>DongFang);






原文地址:https://www.cnblogs.com/wisemen/p/5837863.html