美文网首页Android之界面
Android适配:android percent suppor

Android适配:android percent suppor

作者: lhl_012 | 来源:发表于2016-01-11 17:43 被阅读5383次

1、使用官方的lib,as直接添加依赖:

```

dependencies{   

    compile'com.android.support:percent:24.0.0'

}

```

2、在布局中使用或者,官方暂时没有给出PercentLinearLayout,个人认为这2个足够了,本身用的全是百分比,也没有必要再次使用了。实在想用可以使用:

```

package com.ls.wine.ui;

import android.content.Context;

import android.content.res.TypedArray;

import android.support.percent.PercentLayoutHelper;

import android.util.AttributeSet;

import android.view.ViewGroup;

import android.widget.LinearLayout;

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);

        }

    }

}

```

3、具体用法父布局用,然后在子标签中使用正常的标签,但是

android:layout_width="0dip"和android:layout_height="0dip"一定要写,有些情况不用写,后面会提到,最重要的是app:layout_heightPercent="100%"和app:layout_widthPercent="50%"这2个属性,很明显能读懂,设置就是高度占父控件高度的百分比,宽度占父控件的百分比,这样会根据不同屏幕来设置宽高,不用考虑适配了。

4、扩展(google上面查的方法,很适用)

4.1、设置宽高比例

app:layout_widthPercent="50%"和app:layout_aspectRatio="100%",前面不解释,只是宽度,后面的app:layout_aspectRatio是指宽高比,这样设置就是宽高是1:1,主要是设置哪一个属性(app:layout_heightPercent和app:layout_widthPercent),另外一个就按照写的百分比来,另外一个不要写;当然也可以写固定值,这是官方文档:

```

android:layout_width="300dp"

app:layout_aspectRatio="178%"

```

This will make the aspect ratio 16:9 (1.78:1) with the width fixed at 300dp and height adjusted accordingly.

4.2、可用的属性

layout_widthPercent

layout_heightPercent

layout_marginPercent

layout_marginLeftPercent

layout_marginTopPercent

layout_marginRightPercent

layout_marginBottomPercent

layout_marginStartPercent

layout_marginEndPercent

layout_aspectRatio

4.3、还有N多的方法,原谅林少只能看懂这些,以后有新的再更新

详细请看官方文档:http://developer.android.com/intl/ko/reference/android/support/percent/PercentRelativeLayout.html

mark:林少只是菜B码农,不对的地方希望大家提出来,不要喷哦,原谅我是个嘴强王者。

相关文章

网友评论

本文标题:Android适配:android percent suppor

本文链接:https://www.haomeiwen.com/subject/gmnqkttx.html