<?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>
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);
}
}
<?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>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/cheyiliu/article/details/46770375