当前位置:   article > 正文

Android屏幕适配_android 2030*1080 适配

android 2030*1080 适配

        为啥适配不多赘述,直接上适配方案:

1. 布局适配(layout目录)

        布局适配即根据适配方案之一加载指定布局

  •         layout 默认目录
  •         layout-land 横屏
  •         layout-port 竖屏

        正常情况下,默认目录是竖屏,单独增加layout-land适配横屏,反之亦然

1.1. 宽高限定符

android3.0之前,适配指定分辨率,将layout文件夹做如下命名:
layout-1024 × 768
layout-1024 × 600
layout-1280 × 768

android3.0以后,需将高度减去48像素,即底部状态栏的高度:
layout-976 × 768
layout-976 × 600
layout-1232 × 768

如果要区分横竖屏适配(android3.0以后),目录名加上land (横屏)或port(竖屏)
横屏适配:layout-land-1024 × 720
竖屏适配:layout-port-976 × 768

注意:新版本Android程序(Android10以后)命名有改动:

规则:layout-(屏幕高-48px) × 屏幕宽

但有时此定义仍不准确,建议创建一下三级目录即可适配大部分屏幕:

layout
layout-1920 × 1080
layout-2340 × 1080

1.2. sw限定符

命名如下:
layout-sw360dp
layout-sw392dp
layout-sw411dp

如果要区分横竖屏适配,目录名加上land (横屏)或port(竖屏)

layout-sw360dp-land
layout-sw360dp-port

1.3. w或h限定符

此外还有一种目录layout-w360dp,与layout-sw360dp的区别,举例说明

1.3.1.layout-sw360dp

这里的sw代表smallwidth的意思,当你的屏幕的绝对宽度大于360dp时,屏幕就会自动调用layout-sw360dp文件夹里面的布局。

注意:这里的绝对宽度是指手机的实际宽度,与手机横竖屏无关。sw最小宽度是指屏幕宽高的较小值,每个屏幕都是固定的,不会随着屏幕横向纵向改变而改变。

1.3.2.layout-w360dp

当你的屏幕的相对宽度大于360dp时,屏幕就会自动调用layout-w360dp文件夹里面的布局。

注意:这里的相对宽度是指手机相对放置的宽度;即当手机竖屏时,为较小边的长度;当手机横屏时,为较长边的长度。当屏幕横向纵向切换时,屏幕的宽度是变化的,以变化后的宽度来与原来的宽度相比,看是否使用此资源文件下的资源。

1.3.3.layout-h360dp

与layout-w360dp的使用一样,只是这里指的是相对的高度。但这种方式很少使用,因为屏幕在纵向上通常能够滚动导致长度变化,不像宽度那样基本固定,因为这个方法灵活性不是很好,google 官方文档建议尽量少使用这种方式。

这里的sw、w、h的 dpi 值计算方式如下

  1. DisplayMetrics metrics = getResources().getDisplayMetrics();
  2. int widthDpi = (int) (metrics.widthPixels / metrics.density);
  3. int heightDpi = (int) (metrics.heightPixels / metrics.density);

sw: 取widthDpi 和heightDpi 的较小值
w: widthDpi
h: heightDpi

2. dimen适配(values目录)

        鉴于需要生成多个dimens.xml文件,手动添加的代价过于高昂,而且当前已经有插件可以自动生成,目前推荐使用插件:ScreenMatch

1. ScreenMatch插件安装到Android Studio

和其他插件的安装一样,Android Studio可以通过Plugin Marketplace中查找插件,如下(图中由于我已经安装成功): 

2. dimens文件添加

我们需要添加一分dimens.xml文件作为适配的基准,文件内容主要声明尺寸。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3. <!-- Your custom size defind by references, can be writted in anywhere, any module, any values/*.xml, for example: -->
  4. <dimen name="common_margin">@dimen/dp_15</dimen>
  5. <!-- dp and sp values, must be defind in this file! -->
  6. <!-- view size,you can add if there is no one -->
  7. <dimen name="dp_m_60">-60dp</dimen>
  8. <dimen name="dp_m_30">-30dp</dimen>
  9. <dimen name="dp_m_20">-20dp</dimen>
  10. <dimen name="dp_m_12">-12dp</dimen>
  11. <dimen name="dp_m_10">-10dp</dimen>
  12. <dimen name="dp_m_8">-8dp</dimen>
  13. <dimen name="dp_m_5">-5dp</dimen>
  14. <dimen name="dp_m_2">-2dp</dimen>
  15. <dimen name="dp_m_1">-1dp</dimen>
  16. <dimen name="dp_0">0dp</dimen>
  17. <dimen name="dp_0_1">0.1dp</dimen>
  18. <dimen name="dp_0_5">0.5dp</dimen>
  19. <dimen name="dp_1">1dp</dimen>
  20. <dimen name="dp_1_5">1.5dp</dimen>
  21. <dimen name="dp_2">2dp</dimen>
  22. <dimen name="dp_2_5">2.5dp</dimen>
  23. <dimen name="dp_3">3dp</dimen>
  24. <dimen name="dp_3_5">3.5dp</dimen>
  25. <dimen name="dp_4">4dp</dimen>
  26. <dimen name="dp_4_5">4.5dp</dimen>
  27. <dimen name="dp_5">5dp</dimen>
  28. <dimen name="dp_6">6dp</dimen>
  29. <dimen name="dp_7">7dp</dimen>
  30. <dimen name="dp_8">8dp</dimen>
  31. <dimen name="dp_9">9dp</dimen>
  32. <dimen name="dp_10">10dp</dimen>
  33. <dimen name="dp_11">11dp</dimen>
  34. <dimen name="dp_12">12dp</dimen>
  35. <dimen name="dp_13">13dp</dimen>
  36. <dimen name="dp_14">14dp</dimen>
  37. <dimen name="dp_15">15dp</dimen>
  38. <dimen name="dp_16">16dp</dimen>
  39. <dimen name="dp_17">17dp</dimen>
  40. <dimen name="dp_18">18dp</dimen>
  41. <dimen name="dp_19">19dp</dimen>
  42. <dimen name="dp_20">20dp</dimen>
  43. <dimen name="dp_21">21dp</dimen>
  44. <dimen name="dp_22">22dp</dimen>
  45. <dimen name="dp_23">23dp</dimen>
  46. <dimen name="dp_24">24dp</dimen>
  47. <dimen name="dp_25">25dp</dimen>
  48. <dimen name="dp_26">26dp</dimen>
  49. <dimen name="dp_27">27dp</dimen>
  50. <dimen name="dp_28">28dp</dimen>
  51. <dimen name="dp_29">29dp</dimen>
  52. <dimen name="dp_30">30dp</dimen>
  53. <dimen name="dp_31">31dp</dimen>
  54. <dimen name="dp_32">32dp</dimen>
  55. <dimen name="dp_33">33dp</dimen>
  56. <dimen name="dp_34">34dp</dimen>
  57. <dimen name="dp_35">35dp</dimen>
  58. <dimen name="dp_36">36dp</dimen>
  59. <dimen name="dp_37">37dp</dimen>
  60. <dimen name="dp_38">38dp</dimen>
  61. <dimen name="dp_39">39dp</dimen>
  62. <dimen name="dp_40">40dp</dimen>
  63. <dimen name="dp_41">41dp</dimen>
  64. <dimen name="dp_42">42dp</dimen>
  65. <dimen name="dp_43">43dp</dimen>
  66. <dimen name="dp_44">44dp</dimen>
  67. <dimen name="dp_45">45dp</dimen>
  68. <dimen name="dp_46">46dp</dimen>
  69. <dimen name="dp_47">47dp</dimen>
  70. <dimen name="dp_48">48dp</dimen>
  71. <dimen name="dp_49">49dp</dimen>
  72. <dimen name="dp_50">50dp</dimen>
  73. <dimen name="dp_51">51dp</dimen>
  74. <dimen name="dp_52">52dp</dimen>
  75. <dimen name="dp_53">53dp</dimen>
  76. <dimen name="dp_54">54dp</dimen>
  77. <dimen name="dp_55">55dp</dimen>
  78. <dimen name="dp_56">56dp</dimen>
  79. <dimen name="dp_57">57dp</dimen>
  80. <dimen name="dp_58">58dp</dimen>
  81. <dimen name="dp_59">59dp</dimen>
  82. <dimen name="dp_60">60dp</dimen>
  83. <dimen name="dp_61">61dp</dimen>
  84. <dimen name="dp_62">62dp</dimen>
  85. <dimen name="dp_63">63dp</dimen>
  86. <dimen name="dp_64">64dp</dimen>
  87. <dimen name="dp_65">65dp</dimen>
  88. <dimen name="dp_66">66dp</dimen>
  89. <dimen name="dp_67">67dp</dimen>
  90. <dimen name="dp_68">68dp</dimen>
  91. <dimen name="dp_69">69dp</dimen>
  92. <dimen name="dp_70">70dp</dimen>
  93. <dimen name="dp_71">71dp</dimen>
  94. <dimen name="dp_72">72dp</dimen>
  95. <dimen name="dp_73">73dp</dimen>
  96. <dimen name="dp_74">74dp</dimen>
  97. <dimen name="dp_75">75dp</dimen>
  98. <dimen name="dp_76">76dp</dimen>
  99. <dimen name="dp_77">77dp</dimen>
  100. <dimen name="dp_78">78dp</dimen>
  101. <dimen name="dp_79">79dp</dimen>
  102. <dimen name="dp_80">80dp</dimen>
  103. <dimen name="dp_81">81dp</dimen>
  104. <dimen name="dp_82">82dp</dimen>
  105. <dimen name="dp_83">83dp</dimen>
  106. <dimen name="dp_84">84dp</dimen>
  107. <dimen name="dp_85">85dp</dimen>
  108. <dimen name="dp_86">86dp</dimen>
  109. <dimen name="dp_87">87dp</dimen>
  110. <dimen name="dp_88">88dp</dimen>
  111. <dimen name="dp_89">89dp</dimen>
  112. <dimen name="dp_90">90dp</dimen>
  113. <dimen name="dp_91">91dp</dimen>
  114. <dimen name="dp_92">92dp</dimen>
  115. <dimen name="dp_93">93dp</dimen>
  116. <dimen name="dp_94">94dp</dimen>
  117. <dimen name="dp_95">95dp</dimen>
  118. <dimen name="dp_96">96dp</dimen>
  119. <dimen name="dp_97">97dp</dimen>
  120. <dimen name="dp_98">98dp</dimen>
  121. <dimen name="dp_99">99dp</dimen>
  122. <dimen name="dp_100">100dp</dimen>
  123. <dimen name="dp_101">101dp</dimen>
  124. <dimen name="dp_102">102dp</dimen>
  125. <dimen name="dp_103">103dp</dimen>
  126. <dimen name="dp_104">104dp</dimen>
  127. <dimen name="dp_105">105dp</dimen>
  128. <dimen name="dp_106">106dp</dimen>
  129. <dimen name="dp_107">107dp</dimen>
  130. <dimen name="dp_108">108dp</dimen>
  131. <dimen name="dp_109">109dp</dimen>
  132. <dimen name="dp_110">110dp</dimen>
  133. <dimen name="dp_111">111dp</dimen>
  134. <dimen name="dp_112">112dp</dimen>
  135. <dimen name="dp_113">113dp</dimen>
  136. <dimen name="dp_114">114dp</dimen>
  137. <dimen name="dp_115">115dp</dimen>
  138. <dimen name="dp_116">116dp</dimen>
  139. <dimen name="dp_117">117dp</dimen>
  140. <dimen name="dp_118">118dp</dimen>
  141. <dimen name="dp_119">119dp</dimen>
  142. <dimen name="dp_120">120dp</dimen>
  143. <dimen name="dp_121">121dp</dimen>
  144. <dimen name="dp_122">122dp</dimen>
  145. <dimen name="dp_123">123dp</dimen>
  146. <dimen name="dp_124">124dp</dimen>
  147. <dimen name="dp_125">125dp</dimen>
  148. <dimen name="dp_126">126dp</dimen>
  149. <dimen name="dp_127">127dp</dimen>
  150. <dimen name="dp_128">128dp</dimen>
  151. <dimen name="dp_129">129dp</dimen>
  152. <dimen name="dp_130">130dp</dimen>
  153. <dimen name="dp_131">131dp</dimen>
  154. <dimen name="dp_132">132dp</dimen>
  155. <dimen name="dp_133">133dp</dimen>
  156. <dimen name="dp_134">134dp</dimen>
  157. <dimen name="dp_135">135dp</dimen>
  158. <dimen name="dp_136">136dp</dimen>
  159. <dimen name="dp_137">137dp</dimen>
  160. <dimen name="dp_138">138dp</dimen>
  161. <dimen name="dp_139">139dp</dimen>
  162. <dimen name="dp_140">140dp</dimen>
  163. <dimen name="dp_141">141dp</dimen>
  164. <dimen name="dp_142">142dp</dimen>
  165. <dimen name="dp_143">143dp</dimen>
  166. <dimen name="dp_144">144dp</dimen>
  167. <dimen name="dp_145">145dp</dimen>
  168. <dimen name="dp_146">146dp</dimen>
  169. <dimen name="dp_147">147dp</dimen>
  170. <dimen name="dp_148">148dp</dimen>
  171. <dimen name="dp_149">149dp</dimen>
  172. <dimen name="dp_150">150dp</dimen>
  173. <dimen name="dp_151">151dp</dimen>
  174. <dimen name="dp_152">152dp</dimen>
  175. <dimen name="dp_153">153dp</dimen>
  176. <dimen name="dp_154">154dp</dimen>
  177. <dimen name="dp_155">155dp</dimen>
  178. <dimen name="dp_156">156dp</dimen>
  179. <dimen name="dp_157">157dp</dimen>
  180. <dimen name="dp_158">158dp</dimen>
  181. <dimen name="dp_159">159dp</dimen>
  182. <dimen name="dp_160">160dp</dimen>
  183. <dimen name="dp_161">161dp</dimen>
  184. <dimen name="dp_162">162dp</dimen>
  185. <dimen name="dp_163">163dp</dimen>
  186. <dimen name="dp_164">164dp</dimen>
  187. <dimen name="dp_165">165dp</dimen>
  188. <dimen name="dp_166">166dp</dimen>
  189. <dimen name="dp_167">167dp</dimen>
  190. <dimen name="dp_168">168dp</dimen>
  191. <dimen name="dp_169">169dp</dimen>
  192. <dimen name="dp_170">170dp</dimen>
  193. <dimen name="dp_171">171dp</dimen>
  194. <dimen name="dp_172">172dp</dimen>
  195. <dimen name="dp_173">173dp</dimen>
  196. <dimen name="dp_174">174dp</dimen>
  197. <dimen name="dp_175">175dp</dimen>
  198. <dimen name="dp_176">176dp</dimen>
  199. <dimen name="dp_177">177dp</dimen>
  200. <dimen name="dp_178">178dp</dimen>
  201. <dimen name="dp_179">179dp</dimen>
  202. <dimen name="dp_180">180dp</dimen>
  203. <dimen name="dp_181">181dp</dimen>
  204. <dimen name="dp_182">182dp</dimen>
  205. <dimen name="dp_183">183dp</dimen>
  206. <dimen name="dp_184">184dp</dimen>
  207. <dimen name="dp_185">185dp</dimen>
  208. <dimen name="dp_186">186dp</dimen>
  209. <dimen name="dp_187">187dp</dimen>
  210. <dimen name="dp_188">188dp</dimen>
  211. <dimen name="dp_189">189dp</dimen>
  212. <dimen name="dp_190">190dp</dimen>
  213. <dimen name="dp_191">191dp</dimen>
  214. <dimen name="dp_192">192dp</dimen>
  215. <dimen name="dp_193">193dp</dimen>
  216. <dimen name="dp_194">194dp</dimen>
  217. <dimen name="dp_195">195dp</dimen>
  218. <dimen name="dp_196">196dp</dimen>
  219. <dimen name="dp_197">197dp</dimen>
  220. <dimen name="dp_198">198dp</dimen>
  221. <dimen name="dp_199">199dp</dimen>
  222. <dimen name="dp_200">200dp</dimen>
  223. <dimen name="dp_201">201dp</dimen>
  224. <dimen name="dp_202">202dp</dimen>
  225. <dimen name="dp_203">203dp</dimen>
  226. <dimen name="dp_204">204dp</dimen>
  227. <dimen name="dp_205">205dp</dimen>
  228. <dimen name="dp_206">206dp</dimen>
  229. <dimen name="dp_207">207dp</dimen>
  230. <dimen name="dp_208">208dp</dimen>
  231. <dimen name="dp_209">209dp</dimen>
  232. <dimen name="dp_210">210dp</dimen>
  233. <dimen name="dp_211">211dp</dimen>
  234. <dimen name="dp_212">212dp</dimen>
  235. <dimen name="dp_213">213dp</dimen>
  236. <dimen name="dp_214">214dp</dimen>
  237. <dimen name="dp_215">215dp</dimen>
  238. <dimen name="dp_216">216dp</dimen>
  239. <dimen name="dp_217">217dp</dimen>
  240. <dimen name="dp_218">218dp</dimen>
  241. <dimen name="dp_219">219dp</dimen>
  242. <dimen name="dp_220">220dp</dimen>
  243. <dimen name="dp_221">221dp</dimen>
  244. <dimen name="dp_222">222dp</dimen>
  245. <dimen name="dp_223">223dp</dimen>
  246. <dimen name="dp_224">224dp</dimen>
  247. <dimen name="dp_225">225dp</dimen>
  248. <dimen name="dp_226">226dp</dimen>
  249. <dimen name="dp_227">227dp</dimen>
  250. <dimen name="dp_228">228dp</dimen>
  251. <dimen name="dp_229">229dp</dimen>
  252. <dimen name="dp_230">230dp</dimen>
  253. <dimen name="dp_231">231dp</dimen>
  254. <dimen name="dp_232">232dp</dimen>
  255. <dimen name="dp_233">233dp</dimen>
  256. <dimen name="dp_234">234dp</dimen>
  257. <dimen name="dp_235">235dp</dimen>
  258. <dimen name="dp_236">236dp</dimen>
  259. <dimen name="dp_237">237dp</dimen>
  260. <dimen name="dp_238">238dp</dimen>
  261. <dimen name="dp_239">239dp</dimen>
  262. <dimen name="dp_240">240dp</dimen>
  263. <dimen name="dp_241">241dp</dimen>
  264. <dimen name="dp_242">242dp</dimen>
  265. <dimen name="dp_243">243dp</dimen>
  266. <dimen name="dp_244">244dp</dimen>
  267. <dimen name="dp_245">245dp</dimen>
  268. <dimen name="dp_246">246dp</dimen>
  269. <dimen name="dp_247">247dp</dimen>
  270. <dimen name="dp_248">248dp</dimen>
  271. <dimen name="dp_249">249dp</dimen>
  272. <dimen name="dp_250">250dp</dimen>
  273. <dimen name="dp_251">251dp</dimen>
  274. <dimen name="dp_252">252dp</dimen>
  275. <dimen name="dp_253">253dp</dimen>
  276. <dimen name="dp_254">254dp</dimen>
  277. <dimen name="dp_255">255dp</dimen>
  278. <dimen name="dp_256">256dp</dimen>
  279. <dimen name="dp_257">257dp</dimen>
  280. <dimen name="dp_258">258dp</dimen>
  281. <dimen name="dp_259">259dp</dimen>
  282. <dimen name="dp_260">260dp</dimen>
  283. <dimen name="dp_261">261dp</dimen>
  284. <dimen name="dp_262">262dp</dimen>
  285. <dimen name="dp_263">263dp</dimen>
  286. <dimen name="dp_264">264dp</dimen>
  287. <dimen name="dp_265">265dp</dimen>
  288. <dimen name="dp_266">266dp</dimen>
  289. <dimen name="dp_267">267dp</dimen>
  290. <dimen name="dp_268">268dp</dimen>
  291. <dimen name="dp_269">269dp</dimen>
  292. <dimen name="dp_270">270dp</dimen>
  293. <dimen name="dp_271">271dp</dimen>
  294. <dimen name="dp_272">272dp</dimen>
  295. <dimen name="dp_273">273dp</dimen>
  296. <dimen name="dp_274">274dp</dimen>
  297. <dimen name="dp_275">275dp</dimen>
  298. <dimen name="dp_276">276dp</dimen>
  299. <dimen name="dp_277">277dp</dimen>
  300. <dimen name="dp_278">278dp</dimen>
  301. <dimen name="dp_279">279dp</dimen>
  302. <dimen name="dp_280">280dp</dimen>
  303. <dimen name="dp_281">281dp</dimen>
  304. <dimen name="dp_282">282dp</dimen>
  305. <dimen name="dp_283">283dp</dimen>
  306. <dimen name="dp_284">284dp</dimen>
  307. <dimen name="dp_285">285dp</dimen>
  308. <dimen name="dp_286">286dp</dimen>
  309. <dimen name="dp_287">287dp</dimen>
  310. <dimen name="dp_288">288dp</dimen>
  311. <dimen name="dp_289">289dp</dimen>
  312. <dimen name="dp_290">290dp</dimen>
  313. <dimen name="dp_291">291dp</dimen>
  314. <dimen name="dp_292">292dp</dimen>
  315. <dimen name="dp_293">293dp</dimen>
  316. <dimen name="dp_294">294dp</dimen>
  317. <dimen name="dp_295">295dp</dimen>
  318. <dimen name="dp_296">296dp</dimen>
  319. <dimen name="dp_297">297dp</dimen>
  320. <dimen name="dp_298">298dp</dimen>
  321. <dimen name="dp_299">299dp</dimen>
  322. <dimen name="dp_300">300dp</dimen>
  323. <dimen name="dp_301">301dp</dimen>
  324. <dimen name="dp_302">302dp</dimen>
  325. <dimen name="dp_303">303dp</dimen>
  326. <dimen name="dp_304">304dp</dimen>
  327. <dimen name="dp_305">305dp</dimen>
  328. <dimen name="dp_306">306dp</dimen>
  329. <dimen name="dp_307">307dp</dimen>
  330. <dimen name="dp_308">308dp</dimen>
  331. <dimen name="dp_309">309dp</dimen>
  332. <dimen name="dp_310">310dp</dimen>
  333. <dimen name="dp_311">311dp</dimen>
  334. <dimen name="dp_312">312dp</dimen>
  335. <dimen name="dp_313">313dp</dimen>
  336. <dimen name="dp_314">314dp</dimen>
  337. <dimen name="dp_315">315dp</dimen>
  338. <dimen name="dp_316">316dp</dimen>
  339. <dimen name="dp_317">317dp</dimen>
  340. <dimen name="dp_318">318dp</dimen>
  341. <dimen name="dp_319">319dp</dimen>
  342. <dimen name="dp_320">320dp</dimen>
  343. <dimen name="dp_321">321dp</dimen>
  344. <dimen name="dp_322">322dp</dimen>
  345. <dimen name="dp_323">323dp</dimen>
  346. <dimen name="dp_324">324dp</dimen>
  347. <dimen name="dp_325">325dp</dimen>
  348. <dimen name="dp_326">326dp</dimen>
  349. <dimen name="dp_327">327dp</dimen>
  350. <dimen name="dp_328">328dp</dimen>
  351. <dimen name="dp_329">329dp</dimen>
  352. <dimen name="dp_330">330dp</dimen>
  353. <dimen name="dp_331">331dp</dimen>
  354. <dimen name="dp_332">332dp</dimen>
  355. <dimen name="dp_333">333dp</dimen>
  356. <dimen name="dp_334">334dp</dimen>
  357. <dimen name="dp_335">335dp</dimen>
  358. <dimen name="dp_336">336dp</dimen>
  359. <dimen name="dp_337">337dp</dimen>
  360. <dimen name="dp_338">338dp</dimen>
  361. <dimen name="dp_339">339dp</dimen>
  362. <dimen name="dp_340">340dp</dimen>
  363. <dimen name="dp_341">341dp</dimen>
  364. <dimen name="dp_342">342dp</dimen>
  365. <dimen name="dp_343">343dp</dimen>
  366. <dimen name="dp_344">344dp</dimen>
  367. <dimen name="dp_345">345dp</dimen>
  368. <dimen name="dp_346">346dp</dimen>
  369. <dimen name="dp_347">347dp</dimen>
  370. <dimen name="dp_348">348dp</dimen>
  371. <dimen name="dp_349">349dp</dimen>
  372. <dimen name="dp_350">350dp</dimen>
  373. <dimen name="dp_351">351dp</dimen>
  374. <dimen name="dp_352">352dp</dimen>
  375. <dimen name="dp_353">353dp</dimen>
  376. <dimen name="dp_354">354dp</dimen>
  377. <dimen name="dp_355">355dp</dimen>
  378. <dimen name="dp_356">356dp</dimen>
  379. <dimen name="dp_357">357dp</dimen>
  380. <dimen name="dp_358">358dp</dimen>
  381. <dimen name="dp_359">359dp</dimen>
  382. <dimen name="dp_360">360dp</dimen>
  383. <dimen name="dp_365">365dp</dimen>
  384. <dimen name="dp_370">370dp</dimen>
  385. <dimen name="dp_400">400dp</dimen>
  386. <dimen name="dp_410">410dp</dimen>
  387. <dimen name="dp_422">422dp</dimen>
  388. <dimen name="dp_472">472dp</dimen>
  389. <dimen name="dp_500">500dp</dimen>
  390. <dimen name="dp_600">600dp</dimen>
  391. <dimen name="dp_640">640dp</dimen>
  392. <dimen name="dp_720">720dp</dimen>
  393. <dimen name="dp_376">376dp</dimen>
  394. <dimen name="dp_383">383dp</dimen>
  395. <dimen name="dp_412">412dp</dimen>
  396. <dimen name="dp_420">420dp</dimen>
  397. <dimen name="dp_441">441dp</dimen>
  398. <dimen name="dp_460">460dp</dimen>
  399. <dimen name="dp_552">552dp</dimen>
  400. <dimen name="dp_554">554dp</dimen>
  401. <dimen name="dp_622">622dp</dimen>
  402. <!-- font size,you can add if there is no one -->
  403. <dimen name="sp_6">6sp</dimen>
  404. <dimen name="sp_7">7sp</dimen>
  405. <dimen name="sp_8">8sp</dimen>
  406. <dimen name="sp_9">9sp</dimen>
  407. <dimen name="sp_10">10sp</dimen>
  408. <dimen name="sp_11">11sp</dimen>
  409. <dimen name="sp_12">12sp</dimen>
  410. <dimen name="sp_13">13sp</dimen>
  411. <dimen name="sp_14">14sp</dimen>
  412. <dimen name="sp_15">15sp</dimen>
  413. <dimen name="sp_16">16sp</dimen>
  414. <dimen name="sp_17">17sp</dimen>
  415. <dimen name="sp_18">18sp</dimen>
  416. <dimen name="sp_19">19sp</dimen>
  417. <dimen name="sp_20">20sp</dimen>
  418. <dimen name="sp_21">21sp</dimen>
  419. <dimen name="sp_22">22sp</dimen>
  420. <dimen name="sp_23">23sp</dimen>
  421. <dimen name="sp_24">24sp</dimen>
  422. <dimen name="sp_25">25sp</dimen>
  423. <dimen name="sp_27">27sp</dimen>
  424. <dimen name="sp_28">28sp</dimen>
  425. <dimen name="sp_30">30sp</dimen>
  426. <dimen name="sp_31">31sp</dimen>
  427. <dimen name="sp_32">32sp</dimen>
  428. <dimen name="sp_34">34sp</dimen>
  429. <dimen name="sp_36">36sp</dimen>
  430. <dimen name="sp_38">38sp</dimen>
  431. <dimen name="sp_40">40sp</dimen>
  432. <dimen name="sp_42">42sp</dimen>
  433. <dimen name="sp_44">44sp</dimen>
  434. <dimen name="sp_48">48sp</dimen>
  435. <dimen name="sp_56">56sp</dimen>
  436. <dimen name="sp_64">64sp</dimen>
  437. <dimen name="sp_70">70sp</dimen>
  438. <dimen name="sp_72">72sp</dimen>
  439. </resources>

3. screenMatch生成values

在插件导入之后,可以在values文件夹右键,选择ScreenMatch选项,然后选择在你添加了dimens.xml文件的module下执行,即可生成多个values文件夹。

4. 更新配置

在使用了ScreenMatch时,在Module层级的目录下会有一个ScreenMatch配置文件——screenMatch.properties,在其中可以更新配置,如果要更新需要适配的尺寸,可以在如下的代码中进行变动:

  1. base_dp=600
  2. # Also need to match the phone screen of [match_dp].
  3. # If you have another dp values.
  4. # System default values is 320,360,384,400,432,446.5,480,540,592,600,640,662,720,768,800,820,960,1024,1280,1365

base_dp是你的基准尺寸,可以在System default values中添加你需要适配的尺寸,然后重新执行第三步重新生成values及对应的dimens.xml文件。

5. 布局xml文件中的使用

接下来就是在布局xml文件中去使用大小了,可以采用@dimen/dp_12类似的方式来获取大小。
举例来说,如果要设计一个大小为宽为100dp,高为80dp的按钮,文字大小为20sp,可以在布局中这么写:

  1. <Button
  2. android:layout_width="@dimen/dp_100"
  3. android:layout_height="@dimen/dp_80"
  4. android:textSize="@dimen/sp_20"/>

这样的话,系统就会根据当前设备的宽度dp去对应的dimens.xml文件中查到对应的长度了。

6. 代码中设置大小

代码中的话则需要通过getDimension()方法获取对应资源文件下的大小,工具类如下:

  1. public class ScreenSizeUtil {
  2. /**
  3. * 计算当前的SP的值
  4. * @param context
  5. * @param spSize :R.dimen.sp_16
  6. * @return
  7. */
  8. public static int getSP(Context context,@DimenRes int spSize){
  9. float pxValue = context.getResources().getDimension(spSize);//获取对应资源文件下的sp值
  10. //将px值转换成sp值
  11. return px2sp(context, pxValue);
  12. }
  13. /**
  14. * 计算当前的DP的值
  15. * @param context
  16. * @param dpSize :R.dimen.dp_16
  17. * @return
  18. */
  19. public static int getDP(Context context,@DimenRes int dpSize){
  20. float pxValue = context.getResources().getDimension(dpSize);//获取对应资源文件下的sp值
  21. //将px值转换成sp值
  22. return px2dip(context, pxValue);
  23. }
  24. /**
  25. * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
  26. */
  27. public static int dip2px(Context context, float dpValue) {
  28. if (context == null) {
  29. return (int) dpValue;
  30. }
  31. final float scale = context.getResources().getDisplayMetrics().density;
  32. return (int) (dpValue * scale + 0.5f);
  33. }
  34. /**
  35. * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
  36. */
  37. public static int px2dip(Context context, float pxValue) {
  38. if (context == null) {
  39. return (int) pxValue;
  40. }
  41. final float scale = context.getResources().getDisplayMetrics().density;
  42. return (int) (pxValue / scale + 0.5f);
  43. }
  44. /**
  45. * px转换为sp
  46. * @param context
  47. * @param pxValue
  48. * @return
  49. */
  50. public static int px2sp(Context context,float pxValue){
  51. if (context == null) {
  52. return (int) pxValue;
  53. }
  54. final float scale = context.getResources().getDisplayMetrics().scaledDensity;
  55. return (int) (pxValue / scale + 0.5f);
  56. }
  57. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/270838
推荐阅读
相关标签
  

闽ICP备14008679号