该笔记是学习幕课网上的 屏幕适配做的摘录
作者视频 http://www.imooc.com/video/9348
屏幕尺寸 手机屏幕对角线的长度
屏幕分辨率 480*720
屏幕像素密度 算的是对角线上的像素密度
单位:dpi dot per inch
dp ,dip 密度无关像素
density independent pixels
以 160 dpi 为基准 , 1 dip = 1 px
px = dp*(densityDpi/160)
dp = (px*160)/densityDpi
用代码体现就是:
public static int dip2px(Context context,float dipValue){
final float scale=context.getResouses().getDisplayMetrics().densityDip;
return (int)(dipValue*(scale/160));
}
public static int px2dp(Context context,float pxValue){
final float scale = context.getResource.getDisplayMetrics().densityDip;
return (int)((pxValue*160)/scale);
}
现在常用这样
dp2px
public static int dip2px(Context context,float dipValue){
final float scale=context.getResouses().getDisplayMetrics().density;
return (int)(dipValue*scale+0.5f);
}
px2dp
public static int px2dp(Context context,float pxValue){
final float scale = context.getResource.getDisplayMetrics().density;
return (int)(pxValue/scale+0.5f);
}
sp
scale independent pixel
用于文字的大小设定,Google 建议 大于 12sp
适配
1, 使用 match_parent ,wrap_content , weight
2, 使用 nine 图片
3, 使用相对布局
4, 使用限定符
最小限定符 res/layout-sw600dp/main.xml
其中 sw small width (3.2 平板)
5,布局别名
尺寸限定符
res/layout/main.xml 单面板
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:id="@+id/headlines"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.HeadlinesFragment"
android:layout_width="match_parent" />
</LinearLayout>
res/layout-large/main.xml 双面板
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment android:id="@+id/headlines"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.HeadlinesFragment"
android:layout_width="400dp"
android:layout_marginRight="10dp"/>
<fragment android:id="@+id/article"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.ArticleFragment"
android:layout_width="fill_parent" />
</LinearLayout>
屏幕宽度限定符
res/layout/main.xml,单面板(默认)布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:id="@+id/headlines"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.HeadlinesFragment"
android:layout_width="match_parent" />
</LinearLayout>
res/layout-sw600dp/main.xml,双面板布局: Small Width 最小宽度
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment android:id="@+id/headlines"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.HeadlinesFragment"
android:layout_width="400dp"
android:layout_marginRight="10dp"/>
<fragment android:id="@+id/article"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.ArticleFragment"
android:layout_width="fill_parent" />
</LinearLayout>
使用布局别名
使用布局别名
res/layout/main.xml: 单面板布局
res/layout-large/main.xml: 多面板布局
res/layout-sw600dp/main.xml: 多面板布局
res/layout/main.xml 单面板布局
res/layout/main_twopanes.xml 双面板布局
setContentView(R.layout.main);
默认布局
res/values/layout.xml:
<resources>
<item name="main" type="layout">@layout/main</item>
</resources>
Android3.2之前的平板布局
res/values-large/layout.xml:
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
Android3.2之后的平板布局
res/values-sw600dp/layout.xml:
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
使用屏幕别名
使用屏幕方向限定符
res/values-sw600dp-land/layouts.xml:
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
res/values-sw600dp-port/layouts.xml:
<resources>
<item name="main" type="layout">@layout/main</item>
</resources>
小屏幕,纵向: 1.单面板
小屏幕,横向: 单面板
7 英寸平板电脑,纵向: 2.单面板,带操作栏
7 英寸平板电脑,横向: 3.双面板,宽,带操作栏
10 英寸平板电脑,纵向: 4.双面板,窄,带操作栏
10 英寸平板电脑,横向: 双面板,宽,带操作栏
电视,横向: 双面板,宽,带操作栏
1.res/layout/onepane.xml:(单面板)