标签:
引言: 我相信Android碎片化问题是让所有的Android开发者都比较头疼的问题.尤其是屏幕适配这一块儿.想要自己的app在不同的设备上面都有一个比较好的显示效果.就必须做好相应的屏幕适配.本文是结合网上的相关知识总结、官方文档结合自己的一些理解来进行阐述的.如有不恰当的地方,欢迎斧正.共同学习.
转载请标明出处:http://blog.csdn.net/unreliable_narrator/article/details/51315776
我们先来了解两个概念:屏幕尺寸和屏幕的分辨率:
dpi: dot per inch,就是每英寸的点数。在电子显示范畴内它和PPI是一个意思。计算方式也是和ppi的计算方式是一样的。 只有在打印时这个缩写才有意义,在打印领域不存在 PPI的叫法,只说DPI,它表示打印机每英寸打印几个像素点。宽高同样像素下,dpi越大,打印出来的图案越小。
官方文档自备梯子查看:http://developer.android.com/guide/practices/screens_support.html#range
再来看一个例子。
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.dapeng.autolayout.MainActivity">
<View
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_bright"/>
<View
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_light"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.dapeng.searchdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:textSize="20sp"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:text="普通的小尺寸设备的布局"/>
</RelativeLayout> 再编写res/layout-sw600dp/activity_main:<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.dapeng.searchdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:text="大尺寸尺寸设备的布局"
android:textSize="20sp"/>
</RelativeLayout>我们将该应用程序运行到nexus 5和nexus 7(平板设备)上面看看效果.
public class Constant {
public static int displayWidth; //屏幕宽度
public static int displayHeight; //屏幕高度
} 2.然后在第一个activity启动的时候调用如下代码获取到屏幕的相关参数: 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));
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。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dapeng.searchdemo.MainActivity">
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/top_left"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:background="#ff44aacc"
app:layout_heightPercent="20%"
app:layout_widthPercent="70%"/>
<View
android:id="@+id/top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="@id/top_left"
android:background="#ffe40000"
app:layout_heightPercent="10%"
app:layout_marginLeftPercent="5%"
app:layout_widthPercent="10%"/>
<View
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/top_left"
android:background="#ff00ff22"
app:layout_heightPercent="80%"/>
</android.support.percent.PercentRelativeLayout>
</RelativeLayout>
compile 'com.zhy:autolayout:1.4.3'
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.dapeng.autolayout"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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"/>
</intent-filter>
</activity>
<meta-data
android:name="design_width"
android:value="720">
</meta-data>
<meta-data
android:name="design_height"
android:value="1280">
</meta-data>
</application>
</manifest>
3.在布局文件里面写上UI设计师给定的尺寸参数.单位是px. <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.dapeng.autolayout.MainActivity">
<View
android:layout_width="360px"
android:layout_height="640px"
android:background="@android:color/holo_blue_bright"/>
</RelativeLayout> 4.让activity去继承AutoLayoutActivity就可以了.public class MainActivity extends AutoLayoutActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.zhy.autolayout.AutoRelativeLayout
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"
tools:context="com.dapeng.autolayout.MainActivity">
<View
android:layout_width="360px"
android:layout_height="640px"
android:background="@android:color/holo_blue_bright"/>
<View
android:layout_width="360px"
android:layout_height="640px"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@android:color/holo_orange_light"/>
</com.zhy.autolayout.AutoRelativeLayout>
默认使用的高度是设备的可用高度,也就是不包括状态栏和底部的操作栏的,如果你希望拿设备的物理高度进行百分比化:可以在Application的onCreate方法中进行设置:
public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
AutoLayoutConifg.getInstance().useDeviceSize();
}
}
标签:
原文地址:http://blog.csdn.net/unreliable_narrator/article/details/51315776