Android百分比適配

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>

效果圖如下所示:

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章