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)的設置現(xiàn)在都可以用百分比的方式進行設置了,例如設置控件的寬度layout_width原來我們是這樣玩的android:layout_width="match_parent"現(xiàn)在用了百分比的屬性之后呢,可以這樣玩了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的現(xiàn)在都可以用百分比的方式進行設置了。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 14,206評論 1 92
  • 翻譯自“Auto Layout Guide”。 2 自動布局細則手冊 2.1 堆棧視圖 接下來的章節(jié)展示了如何使用...
    lakerszhy閱讀 1,996評論 3 9
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,391評論 0 17
  • 中午,女兒睡著了。我睡不著,翻來翻去又怕影響孩子睡覺,索性去找老公聊天。 老公在平板上看電視劇吧。 “我們要不要選...
    阿布_771f閱讀 226評論 0 0
  • 一直想看書,可是當書買回來后,放在桌子上,放在書架上,很長時間都不會翻開看。想學攝影,攢了三個月的工資,買了一臺單...
    莫閑2016閱讀 438評論 0 2

友情鏈接更多精彩內(nèi)容