屏幕适配
来源:互联网 发布:资生堂淘宝旗舰店真吗 编辑:程序博客网 时间:2024/06/06 00:51
## 屏幕适配 ##
> 主流屏幕: 1280*720, 遵循原则: 不用AbsoluteLayout(绝对布局), 多用相对布局&线性布局(权重), 要用dp,不用px
> 开发后期, 在不同分辨率屏幕上测试(480*800,1920*1080), 如果没出现太大问题(影响正常使用), 就可以上线
> 如果后期测出问题怎么办?
- 图片适配
根据图片的分辨率,放在不同的drawable文件中,然后根据手机的分辨率来适配
- 布局适配
不是很常用, layout-800x480 专门适配480*800的屏幕,单独写一个布局文件来适配
- 尺寸适配
dp和px的关系: dp = px/设备密度
float density = getResources().getDisplayMetrics().density;
System.out.println("设备密度:" + density);
320*240(0.75), 480*320(1),480*800(1.5),1280*720(2)
> 主流屏幕: 1280*720, 遵循原则: 不用AbsoluteLayout(绝对布局), 多用相对布局&线性布局(权重), 要用dp,不用px
> 开发后期, 在不同分辨率屏幕上测试(480*800,1920*1080), 如果没出现太大问题(影响正常使用), 就可以上线
> 如果后期测出问题怎么办?
- 图片适配
根据图片的分辨率,放在不同的drawable文件中,然后根据手机的分辨率来适配
- 布局适配
不是很常用, layout-800x480 专门适配480*800的屏幕,单独写一个布局文件来适配
- 尺寸适配
dp和px的关系: dp = px/设备密度
float density = getResources().getDisplayMetrics().density;
System.out.println("设备密度:" + density);
320*240(0.75), 480*320(1),480*800(1.5),1280*720(2)
values->dimens.xml values-1280x720
写一个工具类,将dp转px来适配屏幕
package com.ldw.screenAdapter.utils;import android.content.Context;public class DensityUtils {/** * dp转px,屏幕适配 */public static int dp2px(Context ctx, float dp) {float density = ctx.getResources().getDisplayMetrics().density;int px = (int) (dp * density + 0.5f);// 4.9->5 4.4->4return px;}public static float px2dp(Context ctx, int px) {float density = ctx.getResources().getDisplayMetrics().density;float dp = px / density;return dp;}}- 权重适配
android:weightSum="3" // 总的权重,然后再给子部件设置权重来设定占据空间的大
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="3" > <TextView android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" android:background="#0f0" /> <TextView android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" android:background="#00f" /> </LinearLayout>- 代码适配
获取屏幕宽高,动态计算控件大小
package com.ldw.screenAdapter;import android.app.Activity;import android.os.Bundle;import android.widget.LinearLayout.LayoutParams;import android.widget.TextView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);int width = getWindowManager().getDefaultDisplay().getWidth();int height = getWindowManager().getDefaultDisplay().getHeight();TextView tv1 = (TextView) findViewById(R.id.tv_1);TextView tv2 = (TextView) findViewById(R.id.tv_2);LayoutParams params = new LayoutParams(width / 3, (int) (height * 0.2));tv1.setLayoutParams(params);tv2.setLayoutParams(params);}}
阅读全文
0 0
- 【Android 屏幕适配】 屏幕适配
- Android 屏幕适配--屏幕旋转
- COCOS2DX屏幕显示与屏幕适配
- Android屏幕适配-----屏幕尺寸
- Android屏幕尺寸单位/屏幕适配
- 屏幕适配
- 【Android】屏幕适配
- android屏幕适配
- android屏幕适配
- android 屏幕适配
- Android屏幕适配
- Android屏幕适配
- Android屏幕适配
- Android屏幕适配
- android 屏幕适配
- 屏幕适配
- Android屏幕适配
- Android屏幕适配
- SpringMVC数据格式化之日期转换@DateTimeFormat
- Updates were rejected because the tip of your current branch is behindhint: its remote counterpart.
- QueryRunner的使用
- 直接地址,一次间接地址,多次间接地址,索引组织方式
- mysql数据库导入乱码
- 屏幕适配
- DBT-12009&DBT-12001
- centos 7 卸载 mariadb 的正确命令
- Linux上安装Apache环境及安装过程报错解决方案&toastr弹框
- 部署 LAMP (CentOS 7.2)
- C#中参数为引用类型加ref和不加的区别
- apache+tomcat集群及图片共享的解决办法
- spring中注解配置IOC失败异常
- IMWeb提升营Day2 | 训练题8:跳台阶