当前位置:   article > 正文

android中实现设备尺寸适配_android 尺寸适配

android 尺寸适配

1、引言

        设备尺寸适配的重要性想必就不用我多说了,在我发布的历史文章中我曾谈过Qt中的设备尺寸适配问题,那这里我就来教大家如何在android中做设备尺寸适配。在android中设备尺寸适配的方式有好几种,但我喜欢的还是使用获取设备真实尺寸然后按照百分比重新设置控件大小的方式,因此接下来我就只演示这一种方法。

2、准备工作

2.1、生成接口类

        首先需要生成一个接口类,便于区分不同的操作,使用时需要让activity或者fragment继承该接口,具体代码如下:

  1. package xyz.dritrtj.uisize;
  2. public interface Init {
  3. /**
  4. * 初始化视图控件,并设置监听
  5. */
  6. public abstract void initView();
  7. /**
  8. * 设置控件尺寸属性
  9. */
  10. public abstract void setViewSize();
  11. /**
  12. * 设置数据
  13. */
  14. public abstract void setData();
  15. }

2.2、生成UI尺寸工具类

        工具类代码如下,下面会对工具类使用进行讲解:

  1. package xyz.dritrtj.uisize;
  2. import android.graphics.Color;
  3. import android.graphics.drawable.GradientDrawable;
  4. import android.util.TypedValue;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.GridView;
  9. import android.widget.LinearLayout;
  10. import android.widget.RelativeLayout;
  11. import android.widget.TextView;
  12. /**
  13. * 用于适配UI控件尺寸
  14. */
  15. public class SetUiSize {
  16. public static float displayWidth;//屏幕宽度 单位像素
  17. public static float displayHeight;//屏幕高度 单位像素
  18. public static float statusHeight;//状态栏高度 单位像素,暂时没有获取
  19. public static float displayWidthDp = 411;//屏幕默认宽度 单位dp
  20. public static float displayHeightDp = 731;//屏幕默认高度 单位dp
  21. /**
  22. * 设置背景
  23. * @param v
  24. */
  25. public static void setBackground(View v){
  26. GradientDrawable drawable= (GradientDrawable) v.getBackground();
  27. int size= (int) (10/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  28. //两个参数表示一个角,按照左上角,右上角,右下角,左下角的顺序用两个相同的数据表示
  29. drawable.setCornerRadii(new float[]{size,size,size,size,size,size,size,size});
  30. size= (int) (0.38f/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  31. if (size==0){//避免因为数值转化变为零
  32. size=1;
  33. }
  34. drawable.setStroke(size, Color.parseColor("#c5c5c5"));
  35. }
  36. /**
  37. * 设置圆角
  38. * @param v
  39. * @param radius
  40. */
  41. public static void setRadius(View v,int radius){
  42. GradientDrawable drawable= (GradientDrawable) v.getBackground();
  43. int size= (int) (radius/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  44. //两个参数表示一个角,按照左上角,右上角,右下角,左下角的顺序用两个相同的数据表示
  45. drawable.setCornerRadii(new float[]{size,size,size,size,size,size,size,size});
  46. }
  47. /**
  48. * 设置背景
  49. * @param v
  50. * @param strokeColor
  51. */
  52. public static void setBackground(View v,String strokeColor,String solidColor){
  53. GradientDrawable drawable= (GradientDrawable) v.getBackground();
  54. int size= (int) (5/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  55. //两个参数表示一个角,按照左上角,右上角,右下角,左下角的顺序用两个相同的数据表示
  56. drawable.setCornerRadii(new float[]{size,size,size,size,size,size,size,size});
  57. size= (int) (1/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  58. drawable.setStroke(size, Color.parseColor(strokeColor));
  59. drawable.setColor(Color.parseColor(solidColor));
  60. }
  61. /**
  62. * 设置背景
  63. * @param v
  64. * @param strokeColor
  65. * @param solidColor
  66. * @param width
  67. */
  68. public static void setBackground(View v,String strokeColor,String solidColor,int width){
  69. GradientDrawable drawable= (GradientDrawable) v.getBackground();
  70. int size= (int) (5/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  71. //两个参数表示一个角,按照左上角,右上角,右下角,左下角的顺序用两个相同的数据表示
  72. drawable.setCornerRadii(new float[]{size,size,size,size,size,size,size,size});
  73. size= (int) (width/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  74. drawable.setStroke(size, Color.parseColor(strokeColor));
  75. drawable.setColor(Color.parseColor(solidColor));
  76. }
  77. /**
  78. * 设置圆形的选择按钮
  79. * @param v
  80. */
  81. public static void setCircle(View v){
  82. GradientDrawable drawable= (GradientDrawable) v.getBackground();
  83. int size= (int) (16/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  84. drawable.setSize(size,size);
  85. size= (int) (4/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  86. drawable.setStroke(size, Color.parseColor("#dedede"));
  87. }
  88. /**
  89. * 设置TextView字体尺寸
  90. * @param tv
  91. * @param originSize 原始尺寸
  92. */
  93. public static void setTextViewSize(TextView tv, float originSize){
  94. int size= (int) (originSize/displayWidthDp*displayWidth);
  95. tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);//px
  96. // tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,size);//dp
  97. // tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,size);//sp
  98. }
  99. /**
  100. * 设置Button字体尺寸
  101. * @param btn
  102. * @param originSize 原始尺寸
  103. */
  104. public static void setButtonSize(Button btn, float originSize){
  105. int size= (int) (originSize/displayWidthDp*displayWidth);
  106. btn.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);//px
  107. }
  108. /**
  109. * 设置EditText字体尺寸
  110. * @param et
  111. * @param originSize 原始尺寸
  112. */
  113. public static void setEditTextSize(EditText et, float originSize){
  114. int size= (int) (originSize/displayWidthDp*displayWidth);
  115. et.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);//px
  116. }
  117. /**
  118. * 父控件为RelativeLayout时设置底部边距
  119. * @param view
  120. * @param originSize
  121. */
  122. public static void setMarginBottomRelative(View view, float originSize){
  123. int size= (int) (originSize/displayWidthDp*displayWidth);
  124. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  125. layoutParams.bottomMargin=size;
  126. view.setLayoutParams(layoutParams);
  127. }
  128. /**
  129. * 父控件为LinearLayout时设置底部边距
  130. * @param view
  131. * @param originSize
  132. */
  133. public static void setMarginBottomLinear(View view, float originSize){
  134. int size= (int) (originSize/displayWidthDp*displayWidth);
  135. LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
  136. layoutParams.bottomMargin=size;
  137. view.setLayoutParams(layoutParams);
  138. }
  139. /**
  140. * 父控件为RelativeLayout时设置顶部边距
  141. * @param view
  142. * @param originSize
  143. */
  144. public static void setMarginTopRelative(View view, float originSize){
  145. int size= (int) (originSize/displayWidthDp*displayWidth);
  146. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  147. layoutParams.topMargin=size;
  148. view.setLayoutParams(layoutParams);
  149. }
  150. /**
  151. * 父控件为LinearLayout时设置顶部边距
  152. * @param view
  153. * @param originSize
  154. */
  155. public static void setMarginTopLinear(View view, float originSize){
  156. int size= (int) (originSize/displayWidthDp*displayWidth);
  157. LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
  158. layoutParams.topMargin=size;
  159. view.setLayoutParams(layoutParams);
  160. }
  161. /**
  162. * 父控件为RelativeLayout时设置左边距
  163. * @param view
  164. * @param originSize
  165. */
  166. public static void setMarginLeftRelative(View view, float originSize){
  167. int size= (int) (originSize/displayWidthDp*displayWidth);
  168. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  169. layoutParams.leftMargin=size;
  170. view.setLayoutParams(layoutParams);
  171. }
  172. /**
  173. * 父控件为LinearLayout时设置左边距
  174. * @param view
  175. * @param originSize
  176. */
  177. public static void setMarginLeftLinear(View view, float originSize){
  178. int size= (int) (originSize/displayWidthDp*displayWidth);
  179. LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
  180. layoutParams.leftMargin=size;
  181. view.setLayoutParams(layoutParams);
  182. }
  183. /**
  184. * 父控件为RelativeLayout时设置右边距
  185. * @param view
  186. * @param originSize
  187. */
  188. public static void setMarginRightRelative(View view, float originSize){
  189. int size= (int) (originSize/displayWidthDp*displayWidth);
  190. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  191. layoutParams.rightMargin=size;
  192. view.setLayoutParams(layoutParams);
  193. }
  194. /**
  195. * 父控件为LinearLayout时设置右边距
  196. * @param view
  197. * @param originSize
  198. */
  199. public static void setMarginRightLinear(View view, float originSize){
  200. int size= (int) (originSize/displayWidthDp*displayWidth);
  201. LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
  202. layoutParams.rightMargin=size;
  203. view.setLayoutParams(layoutParams);
  204. }
  205. /**
  206. * 父控件为RelativeLayout时设置边距
  207. * @param view
  208. * @param originSize
  209. */
  210. public static void setMarginRelative(View view, float originSize){
  211. setMarginHorizontalRelative(view,originSize);
  212. setMarginVerticalRelative(view,originSize);
  213. }
  214. /**
  215. * 父控件为LinearLayout时设置边距
  216. * @param view
  217. * @param originSize
  218. */
  219. public static void setMarginLinear(View view, float originSize){
  220. setMarginHorizontalLinear(view,originSize);
  221. setMarginVerticalLinear(view,originSize);
  222. }
  223. /**
  224. * 父控件为RelativeLayout时设置layout_marginHorizontal边距
  225. * @param view
  226. * @param originSize
  227. */
  228. public static void setMarginHorizontalRelative(View view, float originSize){
  229. int size= (int) (originSize/displayWidthDp*displayWidth);
  230. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  231. layoutParams.rightMargin=size;
  232. layoutParams.leftMargin=size;
  233. view.setLayoutParams(layoutParams);
  234. }
  235. /**
  236. * 父控件为LinearLayout时设置layout_marginHorizontal边距
  237. * @param view
  238. * @param originSize
  239. */
  240. public static void setMarginHorizontalLinear(View view, float originSize){
  241. int size= (int) (originSize/displayWidthDp*displayWidth);
  242. LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
  243. layoutParams.rightMargin=size;
  244. layoutParams.leftMargin=size;
  245. view.setLayoutParams(layoutParams);
  246. }
  247. /**
  248. * 父控件为RelativeLayout时设置layout_marginVertical边距
  249. * @param view
  250. * @param originSize
  251. */
  252. public static void setMarginVerticalRelative(View view, float originSize){
  253. int size= (int) (originSize/displayWidthDp*displayWidth);
  254. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  255. layoutParams.topMargin=size;
  256. layoutParams.bottomMargin=size;
  257. view.setLayoutParams(layoutParams);
  258. }
  259. /**
  260. * 父控件为LinearLayout时设置layout_marginVertical边距
  261. * @param view
  262. * @param originSize
  263. */
  264. public static void setMarginVerticalLinear(View view, float originSize){
  265. int size= (int) (originSize/displayWidthDp*displayWidth);
  266. LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
  267. layoutParams.topMargin=size;
  268. layoutParams.bottomMargin=size;
  269. view.setLayoutParams(layoutParams);
  270. }
  271. /**
  272. * 父控件为RelativeLayout时设置高度
  273. * @param view
  274. * @param originSize
  275. */
  276. public static void setHeightRelative(View view, float originSize){
  277. int size= (int) (originSize/displayWidthDp*displayWidth);
  278. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  279. layoutParams.height=size;
  280. view.setLayoutParams(layoutParams);
  281. }
  282. /**
  283. * 父控件为LinearLayout时设置高度
  284. * @param view
  285. * @param originSize
  286. */
  287. public static void setHeightLinear(View view, float originSize){
  288. int size= (int) (originSize/displayWidthDp*displayWidth);
  289. LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
  290. layoutParams.height=size;
  291. view.setLayoutParams(layoutParams);
  292. }
  293. /**
  294. * 父控件为RelativeLayout时设置宽度
  295. * @param view
  296. * @param originSize
  297. */
  298. public static void setWidthRelative(View view, float originSize){
  299. int size= (int) (originSize/displayWidthDp*displayWidth);
  300. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  301. layoutParams.width=size;
  302. view.setLayoutParams(layoutParams);
  303. }
  304. /**
  305. * 父控件为LinearLayout时设置宽度
  306. * @param view
  307. * @param originSize
  308. */
  309. public static void setWidthLinear(View view, float originSize){
  310. int size= (int) (originSize/displayWidthDp*displayWidth);
  311. LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
  312. layoutParams.width=size;
  313. view.setLayoutParams(layoutParams);
  314. }
  315. /**
  316. * 设置GridView的verticalSpacing
  317. */
  318. public static void setVerticalSpacing(GridView view, float originSize){
  319. int size= (int) (originSize/displayWidthDp*displayWidth);
  320. view.setVerticalSpacing(size);
  321. }
  322. /**
  323. * 设置padding
  324. */
  325. public static void setPadding(View view, int left, int top, int right, int bottom){
  326. left= (int) (left/displayWidthDp*displayWidth);
  327. top= (int) (top/displayWidthDp*displayWidth);
  328. right= (int) (right/displayWidthDp*displayWidth);
  329. bottom= (int) (bottom/displayWidthDp*displayWidth);
  330. view.setPadding(left,top,right,bottom);
  331. }
  332. }

3、使用方法

3.1、初始化

        首先需要实现接口类,进行相关初始化操作,这里以activity为例,这里最主要的是在setViewSize方法中获取当前设备尺寸,这个一定要放在第一个活动界面中进行初始化,避免后面重复初始化,获取的设备尺寸以像素为单位,代码如下:

  1. package xyz.dritrtj.uisize;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.graphics.Color;
  4. import android.graphics.Point;
  5. import android.os.Bundle;
  6. import android.view.Display;
  7. import android.view.View;
  8. public class UiSizeActivity extends AppCompatActivity implements Init{
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_ui_size);
  13. initView();
  14. setViewSize();
  15. setData();
  16. }
  17. @Override
  18. public void initView() {
  19. //初始化状态栏
  20. View decorView=getWindow().getDecorView();//获取当前界面的DecorView
  21. int option=View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;//更改文字颜色为深黑色
  22. decorView.setSystemUiVisibility(option);//设置系统UI元素的可见性
  23. getWindow().setNavigationBarColor(Color.TRANSPARENT);
  24. getWindow().setStatusBarColor(Color.parseColor("#ffffff"));//将状态栏背景设置为白色
  25. getSupportActionBar().hide();
  26. }
  27. @Override
  28. public void setViewSize() {
  29. //获取屏幕宽度
  30. Display display= getWindowManager().getDefaultDisplay();
  31. Point point=new Point();
  32. display.getSize(point);
  33. SetUiSize.displayWidth=display.getWidth();
  34. SetUiSize.displayHeight=display.getHeight();
  35. }
  36. @Override
  37. public void setData() {
  38. }
  39. }

3.2、修改工具类的dp宽度值

        使用前需要把SetUiSize类前面的displayWidthDp值设置为自己的布局文件宽度值,在布局文件中点击下方图片中的Pixel,鼠标悬浮在Phones按钮上,左边悬浮框在打勾的选项就是布局文件dp尺寸,从左往右小的尺寸就是宽,这里只用到宽就行。

3.3、工具类使用

        工具类在使用时要注意,根布局不能设置尺寸,一定要在根布局之下的布局中设置尺寸,根布局设置为自适应或者match_parent,命名要对照对应的设置操作,如下两个方法:setMarginTopRelative和setMarginTopLinear,前面一个是设置当前控件的顶部边距,且父控件为相对布局,后一个类似,只是父控件为线性布局,这里父控件布局会有影响,请使用对应的布局,如果没有的工具方法,请自行添加。除此之外有一点要注意,布局文件中设置字体大小一定要用sp为单位,其它都用dp为单位,否则会计算错误。

4、效果对照演示

4.1、重设尺寸前

4.2、重设尺寸后

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

闽ICP备14008679号