Android提供了新的适配方案,通过百分比分配控件的大小
具体的实现过程如下:
1.在Android Studio中使用的时候需要在gradle中导入 compile ‘com.android.support:percent:22.2.0’
2.总共提供了两种适配布局:PercentRelativeLayout、PercentFrameLayout
3.提供的属性:
layout_widthPercent, layout_heightPercent, layout_marginPercent, layout_marginLeftPercent, layout_marginTopPercent, layout_marginRightPercent, layout_marginBottomPercent, layout_marginStartPercent, layout_marginEndPercent
这里就不多解释具体的属性
4.使用
布局:
<?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";
xmlns:tools="http://schemas.android.com/tools";
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fiberhome.firstconclusion.MainActivity">
<com.fiberhome.firstconclusion.MyTextView
android:id="@+id/mtv"
android:layout_width="10dp"
android:layout_height="0dp"
app:layout_widthPercent="30%"
app:layout_heightPercent="20%"
app:Text="width 30%;height 20%"
app:TextColor="@android:color/holo_orange_dark"
android:layout_alignParentLeft="true"
app:layout_marginPercent="2%"
app:TextSize="10sp"
/>
<com.fiberhome.firstconclusion.MyTextView
android:id="@+id/mtv1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_widthPercent="60%"
app:layout_heightPercent="20%"
app:Text="width 60%;height 20%"
android:layout_alignParentRight="true"
app:layout_marginPercent="2%"
app:TextColor="@android:color/holo_red_dark"
app:TextSize="15sp"
/>
<com.fiberhome.firstconclusion.MyTextView
android:id="@+id/mtv2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/holo_blue_dark"
app:layout_heightPercent="70%"
app:Text="width 100%;height 70%"
android:layout_alignParentBottom="true"
app:layout_marginPercent="2%"
app:TextColor="@android:color/holo_blue_dark"
app:TextSize="15sp"
/>
</android.support.percent.PercentRelativeLayout>
效果图如下所示: