为啥适配不多赘述,直接上适配方案:
布局适配即根据适配方案之一加载指定布局
正常情况下,默认目录是竖屏,单独增加layout-land适配横屏,反之亦然
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
命名如下:
layout-sw360dp
layout-sw392dp
layout-sw411dp
如果要区分横竖屏适配,目录名加上land (横屏)或port(竖屏)
layout-sw360dp-land
layout-sw360dp-port
此外还有一种目录layout-w360dp,与layout-sw360dp的区别,举例说明
这里的sw代表smallwidth的意思,当你的屏幕的绝对宽度大于360dp时,屏幕就会自动调用layout-sw360dp文件夹里面的布局。
注意:这里的绝对宽度是指手机的实际宽度,与手机横竖屏无关。sw最小宽度是指屏幕宽高的较小值,每个屏幕都是固定的,不会随着屏幕横向纵向改变而改变。
当你的屏幕的相对宽度大于360dp时,屏幕就会自动调用layout-w360dp文件夹里面的布局。
注意:这里的相对宽度是指手机相对放置的宽度;即当手机竖屏时,为较小边的长度;当手机横屏时,为较长边的长度。当屏幕横向纵向切换时,屏幕的宽度是变化的,以变化后的宽度来与原来的宽度相比,看是否使用此资源文件下的资源。
与layout-w360dp的使用一样,只是这里指的是相对的高度。但这种方式很少使用,因为屏幕在纵向上通常能够滚动导致长度变化,不像宽度那样基本固定,因为这个方法灵活性不是很好,google 官方文档建议尽量少使用这种方式。
这里的sw、w、h的 dpi 值计算方式如下
- DisplayMetrics metrics = getResources().getDisplayMetrics();
- int widthDpi = (int) (metrics.widthPixels / metrics.density);
- int heightDpi = (int) (metrics.heightPixels / metrics.density);
sw: 取widthDpi 和heightDpi 的较小值
w: widthDpi
h: heightDpi
鉴于需要生成多个dimens.xml文件,手动添加的代价过于高昂,而且当前已经有插件可以自动生成,目前推荐使用插件:ScreenMatch。
和其他插件的安装一样,Android Studio可以通过Plugin Marketplace中查找插件,如下(图中由于我已经安装成功):
我们需要添加一分dimens.xml文件作为适配的基准,文件内容主要声明尺寸。
- "1.0" encoding="UTF-8"?>
- <resources>
-
-
- <dimen name="common_margin">@dimen/dp_15dimen>
-
-
-
- <dimen name="dp_m_60">-60dpdimen>
- <dimen name="dp_m_30">-30dpdimen>
- <dimen name="dp_m_20">-20dpdimen>
- <dimen name="dp_m_12">-12dpdimen>
- <dimen name="dp_m_10">-10dpdimen>
- <dimen name="dp_m_8">-8dpdimen>
- <dimen name="dp_m_5">-5dpdimen>
- <dimen name="dp_m_2">-2dpdimen>
- <dimen name="dp_m_1">-1dpdimen>
- <dimen name="dp_0">0dpdimen>
- <dimen name="dp_0_1">0.1dpdimen>
- <dimen name="dp_0_5">0.5dpdimen>
- <dimen name="dp_1">1dpdimen>
- <dimen name="dp_1_5">1.5dpdimen>
- <dimen name="dp_2">2dpdimen>
- <dimen name="dp_2_5">2.5dpdimen>
- <dimen name="dp_3">3dpdimen>
- <dimen name="dp_3_5">3.5dpdimen>
- <dimen name="dp_4">4dpdimen>
- <dimen name="dp_4_5">4.5dpdimen>
- <dimen name="dp_5">5dpdimen>
- <dimen name="dp_6">6dpdimen>
- <dimen name="dp_7">7dpdimen>
- <dimen name="dp_8">8dpdimen>
- <dimen name="dp_9">9dpdimen>
- <dimen name="dp_10">10dpdimen>
- <dimen name="dp_11">11dpdimen>
- <dimen name="dp_12">12dpdimen>
- <dimen name="dp_13">13dpdimen>
- <dimen name="dp_14">14dpdimen>
- <dimen name="dp_15">15dpdimen>
- <dimen name="dp_16">16dpdimen>
- <dimen name="dp_17">17dpdimen>
- <dimen name="dp_18">18dpdimen>
- <dimen name="dp_19">19dpdimen>
- <dimen name="dp_20">20dpdimen>
- <dimen name="dp_21">21dpdimen>
- <dimen name="dp_22">22dpdimen>
- <dimen name="dp_23">23dpdimen>
- <dimen name="dp_24">24dpdimen>
- <dimen name="dp_25">25dpdimen>
- <dimen name="dp_26">26dpdimen>
- <dimen name="dp_27">27dpdimen>
- <dimen name="dp_28">28dpdimen>
- <dimen name="dp_29">29dpdimen>
- <dimen name="dp_30">30dpdimen>
- <dimen name="dp_31">31dpdimen>
- <dimen name="dp_32">32dpdimen>
- <dimen name="dp_33">33dpdimen>
- <dimen name="dp_34">34dpdimen>
- <dimen name="dp_35">35dpdimen>
- <dimen name="dp_36">36dpdimen>
- <dimen name="dp_37">37dpdimen>
- <dimen name="dp_38">38dpdimen>
- <dimen name="dp_39">39dpdimen>
- <dimen name="dp_40">40dpdimen>
- <dimen name="dp_41">41dpdimen>
- <dimen name="dp_42">42dpdimen>
- <dimen name="dp_43">43dpdimen>
- <dimen name="dp_44">44dpdimen>
- <dimen name="dp_45">45dpdimen>
- <dimen name="dp_46">46dpdimen>
- <dimen name="dp_47">47dpdimen>
- <dimen name="dp_48">48dpdimen>
- <dimen name="dp_49">49dpdimen>
- <dimen name="dp_50">50dpdimen>
- <dimen name="dp_51">51dpdimen>
- <dimen name="dp_52">52dpdimen>
- <dimen name="dp_53">53dpdimen>
- <dimen name="dp_54">54dpdimen>
- <dimen name="dp_55">55dpdimen>
- <dimen name="dp_56">56dpdimen>
- <dimen name="dp_57">57dpdimen>
- <dimen name="dp_58">58dpdimen>
- <dimen name="dp_59">59dpdimen>
- <dimen name="dp_60">60dpdimen>
- <dimen name="dp_61">61dpdimen>
- <dimen name="dp_62">62dpdimen>
- <dimen name="dp_63">63dpdimen>
- <dimen name="dp_64">64dpdimen>
- <dimen name="dp_65">65dpdimen>
- <dimen name="dp_66">66dpdimen>
- <dimen name="dp_67">67dpdimen>
- <dimen name="dp_68">68dpdimen>
- <dimen name="dp_69">69dpdimen>
- <dimen name="dp_70">70dpdimen>
- <dimen name="dp_71">71dpdimen>
- <dimen name="dp_72">72dpdimen>
- <dimen name="dp_73">73dpdimen>
- <dimen name="dp_74">74dpdimen>
- <dimen name="dp_75">75dpdimen>
- <dimen name="dp_76">76dpdimen>
- <dimen name="dp_77">77dpdimen>
- <dimen name="dp_78">78dpdimen>
- <dimen name="dp_79">79dpdimen>
- <dimen name="dp_80">80dpdimen>
- <dimen name="dp_81">81dpdimen>
- <dimen name="dp_82">82dpdimen>
- <dimen name="dp_83">83dpdimen>
- <dimen name="dp_84">84dpdimen>
- <dimen name="dp_85">85dpdimen>
- <dimen name="dp_86">86dpdimen>
- <dimen name="dp_87">87dpdimen>
- <dimen name="dp_88">88dpdimen>
- <dimen name="dp_89">89dpdimen>
- <dimen name="dp_90">90dpdimen>
- <dimen name="dp_91">91dpdimen>
- <dimen name="dp_92">92dpdimen>
- <dimen name="dp_93">93dpdimen>
- <dimen name="dp_94">94dpdimen>
- <dimen name="dp_95">95dpdimen>
- <dimen name="dp_96">96dpdimen>
- <dimen name="dp_97">97dpdimen>
- <dimen name="dp_98">98dpdimen>
- <dimen name="dp_99">99dpdimen>
- <dimen name="dp_100">100dpdimen>
- <dimen name="dp_101">101dpdimen>
- <dimen name="dp_102">102dpdimen>
- <dimen name="dp_103">103dpdimen>
- <dimen name="dp_104">104dpdimen>
- <dimen name="dp_105">105dpdimen>
- <dimen name="dp_106">106dpdimen>
- <dimen name="dp_107">107dpdimen>
- <dimen name="dp_108">108dpdimen>
- <dimen name="dp_109">109dpdimen>
- <dimen name="dp_110">110dpdimen>
- <dimen name="dp_111">111dpdimen>
- <dimen name="dp_112">112dpdimen>
- <dimen name="dp_113">113dpdimen>
- <dimen name="dp_114">114dpdimen>
- <dimen name="dp_115">115dpdimen>
- <dimen name="dp_116">116dpdimen>
- <dimen name="dp_117">117dpdimen>
- <dimen name="dp_118">118dpdimen>
- <dimen name="dp_119">119dpdimen>
- <dimen name="dp_120">120dpdimen>
- <dimen name="dp_121">121dpdimen>
- <dimen name="dp_122">122dpdimen>
- <dimen name="dp_123">123dpdimen>
- <dimen name="dp_124">124dpdimen>
- <dimen name="dp_125">125dpdimen>
- <dimen name="dp_126">126dpdimen>
- <dimen name="dp_127">127dpdimen>
- <dimen name="dp_128">128dpdimen>
- <dimen name="dp_129">129dpdimen>
- <dimen name="dp_130">130dpdimen>
- <dimen name="dp_131">131dpdimen>
- <dimen name="dp_132">132dpdimen>
- <dimen name="dp_133">133dpdimen>
- <dimen name="dp_134">134dpdimen>
- <dimen name="dp_135">135dpdimen>
- <dimen name="dp_136">136dpdimen>
- <dimen name="dp_137">137dpdimen>
- <dimen name="dp_138">138dpdimen>
- <dimen name="dp_139">139dpdimen>
- <dimen name="dp_140">140dpdimen>
- <dimen name="dp_141">141dpdimen>
- <dimen name="dp_142">142dpdimen>
- <dimen name="dp_143">143dpdimen>
- <dimen name="dp_144">144dpdimen>
- <dimen name="dp_145">145dpdimen>
- <dimen name="dp_146">146dpdimen>
- <dimen name="dp_147">147dpdimen>
- <dimen name="dp_148">148dpdimen>
- <dimen name="dp_149">149dpdimen>
- <dimen name="dp_150">150dpdimen>
- <dimen name="dp_151">151dpdimen>
- <dimen name="dp_152">152dpdimen>
- <dimen name="dp_153">153dpdimen>
- <dimen name="dp_154">154dpdimen>
- <dimen name="dp_155">155dpdimen>
- <dimen name="dp_156">156dpdimen>
- <dimen name="dp_157">157dpdimen>
- <dimen name="dp_158">158dpdimen>
- <dimen name="dp_159">159dpdimen>
- <dimen name="dp_160">160dpdimen>
- <dimen name="dp_161">161dpdimen>
- <dimen name="dp_162">162dpdimen>
- <dimen name="dp_163">163dpdimen>
- <dimen name="dp_164">164dpdimen>
- <dimen name="dp_165">165dpdimen>
- <dimen name="dp_166">166dpdimen>
- <dimen name="dp_167">167dpdimen>
- <dimen name="dp_168">168dpdimen>
- <dimen name="dp_169">169dpdimen>
- <dimen name="dp_170">170dpdimen>
- <dimen name="dp_171">171dpdimen>
- <dimen name="dp_172">172dpdimen>
- <dimen name="dp_173">173dpdimen>
- <dimen name="dp_174">174dpdimen>
- <dimen name="dp_175">175dpdimen>
- <dimen name="dp_176">176dpdimen>
- <dimen name="dp_177">177dpdimen>
- <dimen name="dp_178">178dpdimen>
- <dimen name="dp_179">179dpdimen>
- <dimen name="dp_180">180dpdimen>
- <dimen name="dp_181">181dpdimen>
- <dimen name="dp_182">182dpdimen>
- <dimen name="dp_183">183dpdimen>
- <dimen name="dp_184">184dpdimen>
- <dimen name="dp_185">185dpdimen>
- <dimen name="dp_186">186dpdimen>
- <dimen name="dp_187">187dpdimen>
- <dimen name="dp_188">188dpdimen>
- <dimen name="dp_189">189dpdimen>
- <dimen name="dp_190">190dpdimen>
- <dimen name="dp_191">191dpdimen>
- <dimen name="dp_192">192dpdimen>
- <dimen name="dp_193">193dpdimen>
- <dimen name="dp_194">194dpdimen>
- <dimen name="dp_195">195dpdimen>
- <dimen name="dp_196">196dpdimen>
- <dimen name="dp_197">197dpdimen>
- <dimen name="dp_198">198dpdimen>
- <dimen name="dp_199">199dpdimen>
- <dimen name="dp_200">200dpdimen>
- <dimen name="dp_201">201dpdimen>
- <dimen name="dp_202">202dpdimen>
- <dimen name="dp_203">203dpdimen>
- <dimen name="dp_204">204dpdimen>
- <dimen name="dp_205">205dpdimen>
- <dimen name="dp_206">206dpdimen>
- <dimen name="dp_207">207dpdimen>
- <dimen name="dp_208">208dpdimen>
- <dimen name="dp_209">209dpdimen>
- <dimen name="dp_210">210dpdimen>
- <dimen name="dp_211">211dpdimen>
- <dimen name="dp_212">212dpdimen>
- <dimen name="dp_213">213dpdimen>
- <dimen name="dp_214">214dpdimen>
- <dimen name="dp_215">215dpdimen>
- <dimen name="dp_216">216dpdimen>
- <dimen name="dp_217">217dpdimen>
- <dimen name="dp_218">218dpdimen>
- <dimen name="dp_219">219dpdimen>
- <dimen name="dp_220">220dpdimen>
- <dimen name="dp_221">221dpdimen>
- <dimen name="dp_222">222dpdimen>
- <dimen name="dp_223">223dpdimen>
- <dimen name="dp_224">224dpdimen>
- <dimen name="dp_225">225dpdimen>
- <dimen name="dp_226">226dpdimen>
- <dimen name="dp_227">227dpdimen>
- <dimen name="dp_228">228dpdimen>
- <dimen name="dp_229">229dpdimen>
- <dimen name="dp_230">230dpdimen>
- <dimen name="dp_231">231dpdimen>
- <dimen name="dp_232">232dpdimen>
- <dimen name="dp_233">233dpdimen>
- <dimen name="dp_234">234dpdimen>
- <dimen name="dp_235">235dpdimen>
- <dimen name="dp_236">236dpdimen>
- <dimen name="dp_237">237dpdimen>
- <dimen name="dp_238">238dpdimen>
- <dimen name="dp_239">239dpdimen>
- <dimen name="dp_240">240dpdimen>
- <dimen name="dp_241">241dpdimen>
- <dimen name="dp_242">242dpdimen>
- <dimen name="dp_243">243dpdimen>
- <dimen name="dp_244">244dpdimen>
- <dimen name="dp_245">245dpdimen>
- <dimen name="dp_246">246dpdimen>
- <dimen name="dp_247">247dpdimen>
- <dimen name="dp_248">248dpdimen>
- <dimen name="dp_249">249dpdimen>
- <dimen name="dp_250">250dpdimen>
- <dimen name="dp_251">251dpdimen>
- <dimen name="dp_252">252dpdimen>
- <dimen name="dp_253">253dpdimen>
- <dimen name="dp_254">254dpdimen>
- <dimen name="dp_255">255dpdimen>
- <dimen name="dp_256">256dpdimen>
- <dimen name="dp_257">257dpdimen>
- <dimen name="dp_258">258dpdimen>
- <dimen name="dp_259">259dpdimen>
- <dimen name="dp_260">260dpdimen>
- <dimen name="dp_261">261dpdimen>
- <dimen name="dp_262">262dpdimen>
- <dimen name="dp_263">263dpdimen>
- <dimen name="dp_264">264dpdimen>
- <dimen name="dp_265">265dpdimen>
- <dimen name="dp_266">266dpdimen>
- <dimen name="dp_267">267dpdimen>
- <dimen name="dp_268">268dpdimen>
- <dimen name="dp_269">269dpdimen>
- <dimen name="dp_270">270dpdimen>
- <dimen name="dp_271">271dpdimen>
- <dimen name="dp_272">272dpdimen>
- <dimen name="dp_273">273dpdimen>
- <dimen name="dp_274">274dpdimen>
- <dimen name="dp_275">275dpdimen>
- <dimen name="dp_276">276dpdimen>
- <dimen name="dp_277">277dpdimen>
- <dimen name="dp_278">278dpdimen>
- <dimen name="dp_279">279dpdimen>
- <dimen name="dp_280">280dpdimen>
- <dimen name="dp_281">281dpdimen>
- <dimen name="dp_282">282dpdimen>
- <dimen name="dp_283">283dpdimen>
- <dimen name="dp_284">284dpdimen>
- <dimen name="dp_285">285dpdimen>
- <dimen name="dp_286">286dpdimen>
- <dimen name="dp_287">287dpdimen>
- <dimen name="dp_288">288dpdimen>
- <dimen name="dp_289">289dpdimen>
- <dimen name="dp_290">290dpdimen>
- <dimen name="dp_291">291dpdimen>
- <dimen name="dp_292">292dpdimen>
- <dimen name="dp_293">293dpdimen>
- <dimen name="dp_294">294dpdimen>
- <dimen name="dp_295">295dpdimen>
- <dimen name="dp_296">296dpdimen>
- <dimen name="dp_297">297dpdimen>
- <dimen name="dp_298">298dpdimen>
- <dimen name="dp_299">299dpdimen>
- <dimen name="dp_300">300dpdimen>
- <dimen name="dp_301">301dpdimen>
- <dimen name="dp_302">302dpdimen>
- <dimen name="dp_303">303dpdimen>
- <dimen name="dp_304">304dpdimen>
- <dimen name="dp_305">305dpdimen>
- <dimen name="dp_306">306dpdimen>
- <dimen name="dp_307">307dpdimen>
- <dimen name="dp_308">308dpdimen>
- <dimen name="dp_309">309dpdimen>
- <dimen name="dp_310">310dpdimen>
- <dimen name="dp_311">311dpdimen>
- <dimen name="dp_312">312dpdimen>
- <dimen name="dp_313">313dpdimen>
- <dimen name="dp_314">314dpdimen>
- <dimen name="dp_315">315dpdimen>
- <dimen name="dp_316">316dpdimen>
- <dimen name="dp_317">317dpdimen>
- <dimen name="dp_318">318dpdimen>
- <dimen name="dp_319">319dpdimen>
- <dimen name="dp_320">320dpdimen>
- <dimen name="dp_321">321dpdimen>
- <dimen name="dp_322">322dpdimen>
- <dimen name="dp_323">323dpdimen>
- <dimen name="dp_324">324dpdimen>
- <dimen name="dp_325">325dpdimen>
- <dimen name="dp_326">326dpdimen>
- <dimen name="dp_327">327dpdimen>
- <dimen name="dp_328">328dpdimen>
- <dimen name="dp_329">329dpdimen>
- <dimen name="dp_330">330dpdimen>
- <dimen name="dp_331">331dpdimen>
- <dimen name="dp_332">332dpdimen>
- <dimen name="dp_333">333dpdimen>
- <dimen name="dp_334">334dpdimen>
- <dimen name="dp_335">335dpdimen>
- <dimen name="dp_336">336dpdimen>
- <dimen name="dp_337">337dpdimen>
- <dimen name="dp_338">338dpdimen>
- <dimen name="dp_339">339dpdimen>
- <dimen name="dp_340">340dpdimen>
- <dimen name="dp_341">341dpdimen>
- <dimen name="dp_342">342dpdimen>
- <dimen name="dp_343">343dpdimen>
- <dimen name="dp_344">344dpdimen>
- <dimen name="dp_345">345dpdimen>
- <dimen name="dp_346">346dpdimen>
- <dimen name="dp_347">347dpdimen>
- <dimen name="dp_348">348dpdimen>
- <dimen name="dp_349">349dpdimen>
- <dimen name="dp_350">350dpdimen>
- <dimen name="dp_351">351dpdimen>
- <dimen name="dp_352">352dpdimen>
- <dimen name="dp_353">353dpdimen>
- <dimen name="dp_354">354dpdimen>
- <dimen name="dp_355">355dpdimen>
- <dimen name="dp_356">356dpdimen>
- <dimen name="dp_357">357dpdimen>
- <dimen name="dp_358">358dpdimen>
- <dimen name="dp_359">359dpdimen>
- <dimen name="dp_360">360dpdimen>
- <dimen name="dp_365">365dpdimen>
- <dimen name="dp_370">370dpdimen>
- <dimen name="dp_400">400dpdimen>
- <dimen name="dp_410">410dpdimen>
- <dimen name="dp_422">422dpdimen>
- <dimen name="dp_472">472dpdimen>
- <dimen name="dp_500">500dpdimen>
- <dimen name="dp_600">600dpdimen>
- <dimen name="dp_640">640dpdimen>
- <dimen name="dp_720">720dpdimen>
-
- <dimen name="dp_376">376dpdimen>
- <dimen name="dp_383">383dpdimen>
- <dimen name="dp_412">412dpdimen>
- <dimen name="dp_420">420dpdimen>
- <dimen name="dp_441">441dpdimen>
- <dimen name="dp_460">460dpdimen>
- <dimen name="dp_552">552dpdimen>
- <dimen name="dp_554">554dpdimen>
- <dimen name="dp_622">622dpdimen>
-
-
- <dimen name="sp_6">6spdimen>
- <dimen name="sp_7">7spdimen>
- <dimen name="sp_8">8spdimen>
- <dimen name="sp_9">9spdimen>
- <dimen name="sp_10">10spdimen>
- <dimen name="sp_11">11spdimen>
- <dimen name="sp_12">12spdimen>
- <dimen name="sp_13">13spdimen>
- <dimen name="sp_14">14spdimen>
- <dimen name="sp_15">15spdimen>
- <dimen name="sp_16">16spdimen>
- <dimen name="sp_17">17spdimen>
- <dimen name="sp_18">18spdimen>
- <dimen name="sp_19">19spdimen>
- <dimen name="sp_20">20spdimen>
- <dimen name="sp_21">21spdimen>
- <dimen name="sp_22">22spdimen>
- <dimen name="sp_23">23spdimen>
- <dimen name="sp_24">24spdimen>
- <dimen name="sp_25">25spdimen>
- <dimen name="sp_27">27spdimen>
- <dimen name="sp_28">28spdimen>
- <dimen name="sp_30">30spdimen>
- <dimen name="sp_31">31spdimen>
- <dimen name="sp_32">32spdimen>
- <dimen name="sp_34">34spdimen>
- <dimen name="sp_36">36spdimen>
- <dimen name="sp_38">38spdimen>
- <dimen name="sp_40">40spdimen>
- <dimen name="sp_42">42spdimen>
- <dimen name="sp_44">44spdimen>
- <dimen name="sp_48">48spdimen>
- <dimen name="sp_56">56spdimen>
- <dimen name="sp_64">64spdimen>
- <dimen name="sp_70">70spdimen>
- <dimen name="sp_72">72spdimen>
-
- resources>
-
在插件导入之后,可以在values文件夹右键,选择ScreenMatch选项,然后选择在你添加了dimens.xml文件的module下执行,即可生成多个values文件夹。
在使用了ScreenMatch时,在Module层级的目录下会有一个ScreenMatch配置文件——screenMatch.properties,在其中可以更新配置,如果要更新需要适配的尺寸,可以在如下的代码中进行变动:
- base_dp=600
- # Also need to match the phone screen of [match_dp].
- # If you have another dp values.
- # 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文件。
接下来就是在布局xml文件中去使用大小了,可以采用@dimen/dp_12
类似的方式来获取大小。
举例来说,如果要设计一个大小为宽为100dp,高为80dp的按钮,文字大小为20sp,可以在布局中这么写:
- <Button
- android:layout_width="@dimen/dp_100"
- android:layout_height="@dimen/dp_80"
- android:textSize="@dimen/sp_20"/>
这样的话,系统就会根据当前设备的宽度dp去对应的dimens.xml文件中查到对应的长度了。
代码中的话则需要通过getDimension()
方法获取对应资源文件下的大小,工具类如下:
- public class ScreenSizeUtil {
-
- /**
- * 计算当前的SP的值
- * @param context
- * @param spSize :R.dimen.sp_16
- * @return
- */
- public static int getSP(Context context,@DimenRes int spSize){
-
- float pxValue = context.getResources().getDimension(spSize);//获取对应资源文件下的sp值
- //将px值转换成sp值
- return px2sp(context, pxValue);
- }
-
- /**
- * 计算当前的DP的值
- * @param context
- * @param dpSize :R.dimen.dp_16
- * @return
- */
- public static int getDP(Context context,@DimenRes int dpSize){
- float pxValue = context.getResources().getDimension(dpSize);//获取对应资源文件下的sp值
- //将px值转换成sp值
- return px2dip(context, pxValue);
- }
-
- /**
- * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
- */
- public static int dip2px(Context context, float dpValue) {
- if (context == null) {
- return (int) dpValue;
- }
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dpValue * scale + 0.5f);
- }
-
- /**
- * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
- */
- public static int px2dip(Context context, float pxValue) {
- if (context == null) {
- return (int) pxValue;
- }
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (pxValue / scale + 0.5f);
- }
-
- /**
- * px转换为sp
- * @param context
- * @param pxValue
- * @return
- */
- public static int px2sp(Context context,float pxValue){
- if (context == null) {
- return (int) pxValue;
- }
- final float scale = context.getResources().getDisplayMetrics().scaledDensity;
- return (int) (pxValue / scale + 0.5f);
- }
- }