• Android屏幕适配


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

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

  • 相关阅读:
    骑士CMS01 74cms v4.2.111 后台getshell漏洞复现
    【eNSP】VLAN基础配置
    07 ConfigMap/Secret:怎样配置、定制我的应用
    MySQL 迁移完不能快速导数据了?
    Yew应用中如何获取<textarea/>的值?
    【C++】C++ 引用详解 ⑥ ( 普通变量 / 一级指针 / 二级指针 做函数参数的作用 )
    Appium+Python3安装教程
    odoo 15 会员模块的使用
    Jmeter之BeanShell生成MD5加密数据写入数据库
    数据结构 | 串的存储结构之链串
  • 原文地址:https://blog.csdn.net/weixin_43192102/article/details/126620617