安卓百分比布局的使用: 在gradle中加此行代码 compile 'com.zhy:percent-support-extends:1.0.1'
他有PercentRelativeLayout、PercentFrameLayout两个类,
我们可以根据他们延伸出其他布局 比如线性布局(LinearL
package com.example.mrzhang.myapplication.MyControl;
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;
/**
* Created by MrZhang on 15/6/30.
* 自定义组件 实现线性百分比布局
*/
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);
// }
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<com.example.mrzhang.myapplication.MyControl.PercentLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:gravity="center"
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#000000"
android:text="textview"
android:textColor="#ffffff"
app:layout_heightPercent="10%"
app:layout_widthPercent="100%" />
<TextView
android:id="@+id/tiaozuan"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff0000"
android:text="textview"
android:textColor="#ffffff"
app:layout_heightPercent="10%"
app:layout_widthPercent="100%" />
<Button
android:gravity="center"
android:id="@+id/xiangce"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="相册"
app:layout_heightPercent="10%"
app:layout_widthPercent="100%" />
<ImageView
android:id="@+id/imageview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="30%"
android:layout_gravity="center"
app:layout_widthPercent="50%"/>
<com.example.mrzhang.myapplication.MyAsynchronization.NumberKeyboardView
android:id="@+id/numbers"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:layout_gravity="center_horizontal"
app:layout_heightPercent="40%"
app:layout_widthPercent="100%" />
</com.example.mrzhang.myapplication.MyControl.PercentLinearLayout>
</LinearLayout>