Android5.X之PercentLayout

引入依賴庫

compile 'com.android.support:percent:24.x.x'

主要類

  • PercentRelativeLayout(繼承自RelativeLayout)
  • PercentFrameLayout(繼承自FrameLayout )

屬性

  • layout_widthPercent 設置控件寬度爲父容器的寬的百分比
  • layout_heightPercent 設置控件高度爲父容器的高的百分比
  • layout_marginPercent 設置控件外邊距爲父容器的寬度的百分比
  • layout_marginLeftPercent 設置控件與左邊控件的距離爲父容器的寬度的百分比
  • layout_marginTopPercent 設置控件與上方控件的距離爲父容器的高度的百分比
  • layout_marginRightPercent 設置控件與右邊控件的距離爲父容器的寬度的百分比
  • layout_marginBottomPercent 設置控件與下方控件的距離爲父容器的高度的百分比
  • layout_marginStartPercent
  • layout_marginEndPercent

原來用某些具體單位(如dp)的設置現在都可以用百分比的方式進行設置了,例如設置控件的寬度layout_width原來我們是這樣玩的android:layout_width="match_parent"現在用了百分比的屬性之後呢,可以這樣玩了app:layout_widthPercent="50%",這裏的百分比是相對於父容器而言的。

具體使用

xml文件加入命名空間

xmlns:app="http://schemas.android.com/apk/res-auto"
<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"/>
 
     <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        app:layout_marginTopPercent="25%"
        app:layout_marginLeftPercent="25%"/>
 
 </android.support.percent.PercentRelativeLayout/>

自定義PecentLinearLayout

庫中並沒有提供PecentLinearLayout,想玩PecentLinearLayout就需要自定義了

public class PercentLinearLayout extends LinearLayout
{

    private PercentLayoutHelper mPercentLayoutHelper;

    public PercentLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mPercentLayoutHelper = new PercentLayoutHelper(this);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        mPercentLayoutHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mPercentLayoutHelper.handleMeasuredStateTooSmall())
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
        mPercentLayoutHelper.restoreOriginalParams();
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs)
    {
        return new LayoutParams(getContext(), attrs);
    }

    public static class LayoutParams extends LinearLayout.LayoutParams
            implements PercentLayoutHelper.PercentLayoutParams
    {
        private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;

        public LayoutParams(Context c, AttributeSet attrs)
        {
            super(c, attrs);
            mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);
        }

        @Override
        public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()
        {
            return mPercentLayoutInfo;
        }

        @Override
        protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)
        {
            PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }
    }
}

這樣我們就有了PecentLinearLayout,原來的LinearLayout的現在都可以用百分比的方式進行設置了。

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