com.android.support:percent,Android适配:android percent support lib使用

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码农,不对的地方希望大家提出来,不要喷哦,原谅我是个嘴强王者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值