android 项目学习随笔二十(屏幕适配)
2016-10-26 13:38
555 查看
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
定义不同分辨率的values
在代码中指定宽度长度要进行dp和PX转化,代码所赋的值是像素(px)
4、权重适配
只适合线性布局
android:layout_width="0dp"
把水平宽度等分为3份,两个TextView各占三分之一的宽度
如果weightSum不设置,weightSum的值为子控件权重之和
[b]5、代码适配[/b]
总结:多用相对布局和线性布局(权重), 用dp不用px, 用sp不用px, 代码中如果必须写像素的话, 将dp转为像素之后再设置
放入相同名称的资源文件,机器根据不同分辨率找相近的资源
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的值为子控件权重之和
[b]5、代码适配[/b]
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项目屏幕适配的解决方法
- Android学习-- 屏幕适配
- android 项目学习随笔一(闪屏 )
- android 项目学习随笔九(ListView加头布局)
- 关于Android中涉及屏幕适配的单位dp,px,sp,dip的学习笔记。
- 【Android基础知识】关于屏幕适配的学习(support-screens)
- android 项目学习随笔二(引导页 )
- Android多屏幕适配学习笔记
- android 项目学习随笔七(ViewPagerIndicator与ViewPager)
- android 学习随笔(屏幕的横屏和竖屏)
- android 学习随笔(屏幕的横屏和竖屏)
- android 项目学习随笔三(Fragment )
- Android开源地图项目 BigPlanetTracks 学习随笔
- 【重头学习Android】关于UI切图和屏幕适配问题
- Android屏幕适配-android学习之旅(五十九)
- Android屏幕适配-android学习之旅(五十九)
- android 项目学习随笔六(网络缓存)
- android 项目学习随笔四(优化ViewPager)
- android 学习随笔二十(多媒体编程 )