为了保证用户获得一致的用户体验效果,使得某一元素在Android不同尺寸、不同分辨率的手机上具备相同的显示效果,则需要我们进行屏幕适配。
屏幕尺寸是指屏幕对角线的长度,单位是英寸,1 inch=2.54 cm
手机在横向和纵向上的像素点数总和,单位是像素(pixel),1px = 1像素点,举个栗子,1080x1920,即宽度方向上有1080个像素点,在高度方向上有1920个像素点。
每英寸像素点个数,单位是dpi,dots per inch。为简便起见,Android 将所有屏幕密度分组为六种通用密度: 低、中、高、超高、超超高和超超超高。
Density Independent Pixels,即密度无关像素。
Scale Independent Pixels, 即sp或sip。
Android开发时用此单位设置文字大小,可根据字体大小首选项进行缩放,推荐使用12sp、14sp、18sp、22sp作为字体设置的大小,不推荐使用奇数和小数,容易造成精度的丢失问题,小于12sp的字体会太小导致用户看不清。
在设计图标时,对于5种主流的像素密度(mdpi,hdpi,xhdpi,xxhdpi和xxxdpi)应按照2:3:4:6:8的比例进行缩放。例如一个启动图片ic_launcher.png,它在各个像素密度文件夹下大小为:
根据加载图片的流程,可以得出理论上提供一套图片就可以了。
原则上越高越好,同时结合当前主流分辨率屏幕
使用wrap_content, match_parent, layout_weight。
当layout_width为0dp,layout_weight分别是1和2
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="weight = 1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="weight = 2"/>
</LinearLayout>
当layout_width为match_parent,layout_weight分别为1和2
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="weight = 1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="weight = 2"/>
</LinearLayout>
宽度 = 原来宽度 + 权重比值 * 剩余宽度
当layout_width为0dp,layout_weight分别是1和2
第一个按钮:宽度 = 0 + 1/3 * 屏宽 = 1/3屏宽
第二个按钮:宽度 = 0 + 2/3 * 屏宽 = 2/3屏宽
当layout_width为match_parent, layout_weight分别是1和2
第一个按钮:宽度 = 屏宽 + 1/3 (屏宽 - 2 屏宽) = 2/3屏宽
第二个按钮:宽度 = 屏宽 + 2/3 (屏宽 - 2 屏宽) = 1/3屏宽
使用相对布局,禁用绝对布局。
最后的两个文件的xml内容是完全相同的,这会带来:文件名的重复从而带来一些列后期维护的问题,修改一个文件,可能忘记修改另外一个。于是为了要解决这种重复问题,我们引入了布局别名。
适配尺寸>7寸平板的双面板布局:res/layout/activity_twopanes.xml
res/values/layout.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="main" type="layout">@layout/activity_main</item>
</resources>
res/values-large/layout.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="main" type="layout">@layout/activity_twopanes</item>
</resources>
res/values-sw600dp/layout.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="main" type="layout">@layout/activity_twopanes</item>
</resources>
setContentView(R.layout.main);
即使使用dp,依然不能解决屏幕分辨率的适配问题,我们可以针对不同的屏幕创建不同的dimen值。
res/values/dimens.xml
<resources>
<dimen name="button_length_1">180dp</dimen>
<dimen name="button_length_2">160dp</dimen>
</resources>
res/values-480x800/dimens.xml
<resources>
<dimen name="button_length_1">113dp</dimen>
<dimen name="button_length_2">100dp</dimen>
</resources>
Github Sample
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="30%"
app:layout_widthPercent="30%"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="20%"
app:layout_widthPercent="20%"/>
</android.support.percent.PercentRelativeLayout>
当NewsReader在横屏时是双面板,左侧是HeadLinesFragment, 右侧是ArticleFragment, 点击新闻标题, 切换ArticleFragment的内容。当NewsReader在竖屏时是单面板,只有个HeadLinesFragment, 点击新闻标题,跳转到ArticleActivity去显示新闻内容。所以,要实现这样的横竖屏适配,只是通过布局是完成不了的,不同业务逻辑的处理,还需要写代码来完成,这就是我们的自适应用户界面。
res/values/layouts.xml
<resources>
<item name="main_layout" type="layout">@layout/onepane_with_bar</item>
<bool name="has_two_panes">false</bool>
</resources>
res/values-sw600dp-land/layouts.xml
<resources>
<item name="main_layout" type="layout">@layout/twopanes</item>
<bool name="has_two_panes">true</bool>
</resources>
res/values-sw600dp-port/layouts.xml
<resources>
<item name="main_layout" type="layout">@layout/onepane</item>
<bool name="has_two_panes">false</bool>
</resources>
View articleView = findViewById(R.id.article);
mIsDualPane = articleView != null && articleView.getVisibility() == View.VISIBLE;//如果能够找到ArticleFragment则是双面板
public void onHeadlineSelected(int index) {
mArtIndex = index;
if (mIsDualPane) {
// display it on the article fragment
mArticleFragment.displayArticle(mCurrentCat.getArticle(index));
}
else {
// use separate activity
Intent i = new Intent(this, ArticleActivity.class);
i.putExtra("catIndex", mCatIndex);
i.putExtra("artIndex", index);
startActivity(i);
}
}
Android屏幕适配实现技巧转载http://www.codesocang.com/anzhuoyuanma/boke/33931.html