android 项目学习随笔二十(屏幕适配)
1、图片适配

放入相同名称的资源文件,机器根据不同分辨率找相近的资源
240*320 ldpi
320*480 mdpi
480*800 hdpi
720*1280 xhdpi
2、布局适配
在不同的分辨率下显示不同的布局,定义不同分辨率的布局文件,一般控件相同(否则FindViewByID找不到)
layout-800x480, 适配480*800分辨率的布局
3、尺寸适配
dp 设备独立像素
dp = px / 设备密度
float density = getResources().getDisplayMetrics().density;
System.out.println("设备密度:" + density);
分辨率 设备密度 像素(PX)
240*320 0.75 120px
320*480 1.0 160px
480*800 1.5 240px
1280*720 2 320px (主流屏幕)
1920*1080
设置为DP后,系统运行起来后会自动计算出像素
/res/values/dimens.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="textview_width">160dp</dimen>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#f00" /> <TextView
android:layout_width="@dimen/textview_width"
android:layout_height="50dp"
android:background="#0f0" /> </LinearLayout>
定义不同分辨率的values
import android.content.Context;
public class DensityUtils {
public static int dp2px(float dp, Context ctx) {
float density = ctx.getResources().getDisplayMetrics().density;
// 4.1->4, 4.9->4
int px = (int) (dp * density + 0.5f);// 加0.5可以四舍五入
return px;
}
public static float px2dp(int px, Context ctx) {
float density = ctx.getResources().getDisplayMetrics().density;
float dp = px / density;
return dp;
}
}
在代码中指定宽度长度要进行dp和PX转化,代码所赋的值是像素(px)
4、权重适配
只适合线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3" > <TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#f00" /> <TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#0f0" />
</LinearLayout> </LinearLayout>
android:layout_width="0dp"
把水平宽度等分为3份,两个TextView各占三分之一的宽度
如果weightSum不设置,weightSum的值为子控件权重之和
5、代码适配
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
float density = getResources().getDisplayMetrics().density;
System.out.println("设备密度:" + density);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
TextView tvText = (TextView) findViewById(R.id.tv_text);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
width / 3, height / 10);
tvText.setLayoutParams(params);
}
总结:多用相对布局和线性布局(权重), 用dp不用px, 用sp不用px, 代码中如果必须写像素的话, 将dp转为像素之后再设置
android 项目学习随笔二十(屏幕适配)的更多相关文章
- android 项目学习随笔二十一(IM、语音识别、机器人、统计、扫描二维码、条形码)
语音识别:科大讯飞语音云 http://www.xfyun.cn/ 语音机器人模拟 public class TalkBean { public String text; public boolean ...
- android 项目学习随笔二(引导页 )
1.引导页 1.定义灰色.红色圆点的shape XML文件 2.定义布局文件,利用相对布局文件定位,利用线性布局加载灰色圆点,imageview加载红色圆点 3.android.support.v4. ...
- (转载)Android项目实战(二十八):使用Zxing实现二维码及优化实例
Android项目实战(二十八):使用Zxing实现二维码及优化实例 作者:听着music睡 字体:[增加 减小] 类型:转载 时间:2016-11-21我要评论 这篇文章主要介绍了Android项目 ...
- (转载)Android项目实战(二十八):Zxing二维码实现及优化
Android项目实战(二十八):Zxing二维码实现及优化 前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中 ...
- android 学习随笔二十二(小结)
ADB进程 * adb指令 * adb install xxx.apk * adb uninstall 包名 * adb devices * adb start-server * adb kill-s ...
- Android项目实战(二十四):项目包成jar文件,并且将工程中引用的jar一起打入新的jar文件中
前言: 关于.jar文件: 平时我们Android项目开发中经常会用到第三方的.jar文件. 其实.jar文件就是一个类似.zip文件的压缩包,里面包含了一些源代码,注意的是.jar不包含资源文件(r ...
- Android项目实战(二十八):Zxing二维码实现及优化
前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中我们也许只会用到二维码的扫描和生成两个功能,所以不必下载完整的ja ...
- Android项目实战(二十五):Android studio 混淆+打包+验证是否成功
前言: 单挑Android项目,最近即时通讯用到环信,集成sdk的时候 官方有一句 在 ProGuard 文件中加入以下 keep. -keep class com.hyphenate.** {*;} ...
- Android项目实战(二十九):酒店预定日期选择
先看需求效果图: 几个需求点: 1.显示当月以及下个月的日历 (可自行拓展更多月份) 2.首次点击选择"开始日期",再次点击选择"结束日期" (1).如果&qu ...
随机推荐
- opacity_不透明度 cursor_鼠标形状
1.不透明 opacity:0.5; (不透明度为50%) filter:alpha(opacity=50);(为了兼容IE) 2.鼠标形状 cursor 属性规定要显示的光标的类型(形状). ...
- Set Java Proxy for Http/Https
Command Line JVM Settings The proxy settings are given to the JVM via command line arguments: java ...
- python判断key是否在字典用in不用has_key
小测试 in del.py import datetime cur = datetime.datetime.now() num = 1 a_list = {"a":1, " ...
- Java最全文件操作实例汇总
本文实例汇总了Java文件操作.分享给大家供大家参考,具体如下: 1.创建文件夹 ? 1 2 3 4 5 6 7 8 9 10 11 //import java.io.*; File myFolder ...
- mysql的binlog安全删除
理论上,应该在配置文件/etc/my.cnf中加上binlog过期时间的配置项,expire_logs_days = 10 但是如果没有加这一项,随着产生越来越多的binlog,磁盘被吃掉了不少.可以 ...
- 在navgationController中添加UISegmentedControl
NSArray *segmentedArray = [NSArray arrayWithObjects:@"患者基本信息",@"患者信息",nil]; UISe ...
- C#面向对象的方法写数组的功能
上一篇文章用Java方法写出了可以对数组执行的功能,然后在用实例化后的对象调用这些方法来实现这些功能: 这篇随笔改用C#语言实现同样的功能 方法类:Array using System; using ...
- How to export a template in Visual Studio?
Create a customize template file: 1.template arguments introduction like: 上图只是其中一部分,更多请查看文后的参考资源 tem ...
- nyist 506 洗澡
http://acm.nyist.net/JudgeOnline/problem.php?pid=506 洗澡 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 Mos ...
- SQL中索引的原理
(一)深入浅出理解索引结构 实际上,您可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引 ...