android 百分比控件的使用

概述

  • 谷歌官方推出這個百分比庫對android的屏幕適配肯定有很大的幫助,當然具體好不好用還得根據不同的使用場景來分析。
  • 這個支持包裏的內容有:百分比相對佈局PercentRelativeLayout,百分比幀佈局PercentFrameLayout,百分比線性佈局PercentLinearLayout。

先跑demo

代碼分析和使用

庫的實現

  • 自定義屬性,支持的屬性包括:百分比寬高和百分比margin
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="PercentLayout_Layout">
        <attr name="layout_widthPercent" format="string" />
        <attr name="layout_heightPercent" format="string" />
        <attr name="layout_marginPercent" format="string" />
        <attr name="layout_marginLeftPercent" format="string" />
        <attr name="layout_marginTopPercent" format="string" />
        <attr name="layout_marginRightPercent" format="string" />
        <attr name="layout_marginBottomPercent" format="string" />
        <attr name="layout_marginStartPercent" format="string" />
        <attr name="layout_marginEndPercent" format="string" />
    </declare-styleable>

</resources>
  • 繼承現有控件並應用自定義屬性。android view的佈局和繪製要大致經歷onMeasure onLayout onDraw幾步,那百分比屬性的應用肯定是在onMeasure過程中了。以PercentRelativeLayout爲例簡單過下代碼。
1.類PercentLayoutInfo用來存儲自定義的百分比屬性,可理解爲百分比屬性model。
2.類LayoutParams,繼承原有的layoutparam並持有百分比屬性model類。在構造時完成對model的賦值。
    public static class LayoutParams extends RelativeLayout.LayoutParams
            implements PercentLayoutHelper.PercentLayoutParams {
        private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);
        }
3.在onMeasure中完成對百分比屬性model的應用,helper的代碼就不展開看了。
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mHelper.handleMeasuredStateTooSmall()) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

使用庫

  • 首先建立和庫工程的依賴關係
  • 在layout文件中使用
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:text="percent relative layout1"
        android:id="@+id/top_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%"
        android:background="#ff44aacc" />

    <View
        android:id="@+id/top_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/top_left"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="30%"
        android:background="#ffe40000" />

    <View
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/top_left"
        app:layout_heightPercent="80%"
        android:background="#ff00ff22" />

</android.support.percent.PercentRelativeLayout>

效果圖

1
2
3

更多參考

發佈了113 篇原創文章 · 獲贊 6 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章