android 百分比控制項的使用,android百分比控制項

來源:互聯網
上載者:User

android 百分比控制項的使用,android百分比控制項
概述

  • Google官方推出這個百分比庫對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>



更多參考
  • https://github.com/cheyiliu/test4XXX/tree/master/test4androidSupportPercentLayout
  • https://github.com/cheyiliu/test4XXX/tree/master/androidSupportPercent
  • http://blog.csdn.net/lmj623565791/article/details/46695347
  • https://github.com/hongyangAndroid/android-percent-support-extend
  • https://github.com/JulienGenoud/android-percent-support-lib-sample

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.