引言: 我相信Android碎片化问题是让所有的Android开发者都比较头疼的问题.尤其是屏幕适配这一块儿.想要自己的app在不同的设备上面都有一个比较好的显示效果.就必须做好相应的屏幕适配.本文是结合网上的相关知识总结、官方文档结合自己的一些理解来进行阐述的.如有不恰当的地方,欢迎斧正.共同学习.
我们先来了解两个概念:屏幕尺寸和屏幕的分辨率:
dpi: dot per inch,就是每英寸的点数。在电子显示范畴内它和PPI是一个意思。计算方式也是和ppi的计算方式是一样的。 只有在打印时这个缩写才有意义,在打印领域不存在 PPI的叫法,只说DPI,它表示打印机每英寸打印几个像素点。宽高同样像素下,dpi越大,打印出来的图案越小。
官方文档自备梯子查看:http://developer.android.com/guide/practices/screens_support.html
再来看一个例子。
1
2
|
<linearlayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
android:orientation=
"horizontal"
tools:context=
"com.dapeng.autolayout.MainActivity"
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
><view android:background=
"@android:color/holo_blue_bright"
android:layout_height=
"20dp"
android:layout_weight=
"1"
android:layout_width=
"0dp"
> <view android:background=
"@android:color/holo_orange_light"
android:layout_height=
"20dp"
android:layout_weight=
"1"
android:layout_width=
"0dp"
>
</view></view></linearlayout>
|
方案二.使用限定符:
我们将该应用程序运行到nexus 5和nexus 7(平板设备)上面看看效果.
1
2
3
4
|
public
class
Constant {
public
static
int
displayWidth;
//屏幕宽度
public
static
int
displayHeight;
//屏幕高度
}
|
1
2
3
4
5
6
|
mView = findViewById(R.id.v);
DisplayMetrics displayMetrics =
new
DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
Constant.displayWidth = displayMetrics.widthPixels;
Constant.displayHeight = displayMetrics.heightPixels;
startActivity(
new
Intent(MainActivity.
this
, ActivityTwo.
class
));
|
缺点:可见性差,维护非常的繁琐。
1
|
compile
'com.android.support:percent:23.3.0'
|
layout_widthPercent、layout_heightPercent、
layout_marginPercent、layout_marginLeftPercent、
layout_marginTopPercent、layout_marginRightPercent、
layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。
1
2
3
4
5
6
|
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<relativelayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
tools:context=
"com.dapeng.searchdemo.MainActivity"
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:app=
"http://schemas.android.com/apk/res-auto"
xmlns:tools=
"http://schemas.android.com/tools"
>
<android.support.percent.percentrelativelayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<view android:background=
"#ff44aacc"
android:id=
"@+id/top_left"
android:layout_alignparenttop=
"true"
android:layout_height=
"0dp"
android:layout_width=
"0dp"
app:layout_heightpercent=
"20%"
app:layout_widthpercent=
"70%"
>
<view android:background=
"#ffe40000"
android:id=
"@+id/top_right"
android:layout_height=
"0dp"
android:layout_torightof=
"@id/top_left"
android:layout_width=
"0dp"
app:layout_heightpercent=
"10%"
app:layout_marginleftpercent=
"5%"
app:layout_widthpercent=
"10%"
>
<view android:background=
"#ff00ff22"
android:id=
"@+id/bottom"
android:layout_below=
"@+id/top_left"
android:layout_height=
"0dp"
android:layout_width=
"match_parent"
app:layout_heightpercent=
"80%"
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<manifest
package
=
"com.dapeng.autolayout"
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<application android:allowbackup=
"true"
android:icon=
"@mipmap/ic_launcher"
android:label=
"@string/app_name"
android:name=
".MyApplication"
android:supportsrtl=
"true"
android:theme=
"@style/AppTheme"
>
<activity android:name=
".MainActivity"
>
<intent-filter>
<action android:name=
"android.intent.action.MAIN"
>
<category android:name=
"android.intent.category.LAUNCHER"
>
</category></action></intent-filter>
</activity>
<meta-data android:name=
"design_width"
android:value=
"720"
>
<meta-data android:name=
"design_height"
android:value=
"1280"
>
</meta-data></meta-data></application><meta-data android:name=
"design_width"
android:value=
"720"
><meta-data android:name=
"design_height"
android:value=
"1280"
><meta-data android:name=
"design_width"
android:value=
"720"
><meta-data android:name=
"design_height"
android:value=
"1280"
>
|
3.在布局文件里面写上UI设计师给定的尺寸参数.单位是px.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<relativelayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
tools:context=
"com.dapeng.autolayout.MainActivity"
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
>
<view android:background=
"@android:color/holo_blue_bright"
android:layout_height=
"640px"
android:layout_width=
"360px"
>
</view></relativelayout></pre>
4
.让activity去继承AutoLayoutActivity就可以了.<br />
<pre
class
=
"brush:java;"
>
public
class
MainActivity
extends
AutoLayoutActivity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
}
}
|
- LinearLayout -> AutoLinearLayout
- RelativeLayout -> AutoRelativeLayout
- FrameLayout -> AutoFrameLayout
- layout_width
- layout_height
- layout_margin(left,top,right,bottom)
- pading(left,top,right,bottom)
- textSize
- maxWidth, minWidth, maxHeight, minHeight
默认使用的高度是设备的可用高度,也就是不包括状态栏和底部的操作栏的,如果你希望拿设备的物理高度进行百分比化:可以在Application的onCreate方法中进行设置:
1
2
3
4
5
6
7
8
9
|
public
class
MyApplication
extends
Application
{
@Override
public
void
onCreate()
{
super
.onCreate();
AutoLayoutConifg.getInstance().useDeviceSize();
}
}
|