在application中添加代码:
@Override
public void onCreate() {
super.onCreate();
resetDensity();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
resetDensity();
}
public final static float DESIGN_WIDTH = 1440;
public void resetDensity() {
Point size = new Point();
((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getSize(size);
getResources().getDisplayMetrics().xdpi = size.x / DESIGN_WIDTH * 72f;
}
其中DESIGN_WIDTH是自定义的屏幕宽度,代表手机总得宽度为1440pt。
例子:
<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"
tools:context="animate.han.com.ptadapter.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="750pt"
android:text="@string/title1"
android:textColor="#fff"
android:textAllCaps="false"
android:background="#5f5f5f"
android:layout_height="750pt"/>
<Button
android:layout_width="match_parent"
android:text="@string/title2"
android:textColor="#fff"
android:background="#ffff00"
android:textAllCaps="false"
android:layout_marginTop="10pt"
android:layout_height="80pt"/>
<Button
android:layout_width="match_parent"
android:text="@string/title3"
android:textAllCaps="false"
android:textColor="#fff"
android:background="#5f5f5f"
android:layout_marginTop="5pt"
android:layout_height="80pt"/>
</LinearLayout>
对于8.0系统展示的时候有问题,显示的会与预览出来的效果一样,没解决。