使用百分比布局,首先我们使用android studio为例
在gradle中添加
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'//25.3.0
compile 'com.android.support:percent:25.3.0'//使用百分比布局加包,版本与上相同25.3.0
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
testCompile 'junit:junit:4.12'
}
这样就可以使用了,直接上代码
//PercentFrameLayout 也是fragmelayout的一种,所以是左上角对其
//当然可以使用PercentRelativeLayout百分比相对布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"//加入app的声明
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="button1"
android:layout_gravity="right|bottom"//控件所在位置
app:layout_widthPercent="50%"//控件占用的百分比宽
app:layout_heightPercent="50%"//控件占用的百分比高
/>
</android.support.percent.PercentFrameLayout>
下面是编写代码的实现
public class PercentRelativeLayout extends RelativeLayout{
public PercentRelativeLayout(Context context) {
super(context);
}
public PercentRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public PercentRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 重写测量方法
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 先拿到父控件的宽高
int width = View.MeasureSpec.getSize(widthMeasureSpec);
int height = View.MeasureSpec.getSize(heightMeasureSpec);
int count = this.getChildCount();
for (int i = 0; i < count; i++) {// 循环迭代子控件
View child = this.getChildAt(i);// 取出每一个子控件
ViewGroup.LayoutParams lp = child.getLayoutParams();
float widthPercent = 0;
float hightPercent = 0;
if (lp instanceof PercentRelativeLayout.LayoutParams) {// 支持百分比布局
widthPercent = ((PercentRelativeLayout.LayoutParams) lp).widthPercent;
hightPercent = ((PercentRelativeLayout.LayoutParams) lp).heightPercent;
}
if (widthPercent != 0) {
// 父容器的宽*宽的百分比
lp.width = (int) (width * widthPercent);
}
if (hightPercent != 0) {
// 父容器的高*高的百分比
lp.height = (int) (height * hightPercent);
}
}
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);
}
/**
* 重写对子控件布局属性进行获取解析
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
// return super.generateLayoutParams(attrs);// 这里必须返回下面自定的LayoutParams
return new LayoutParams(getContext(), attrs);
}
public static class LayoutParams extends RelativeLayout.LayoutParams{
private float widthPercent;
private float heightPercent;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs,R.styleable.precentRelativeLayout);
widthPercent = a.getFloat(R.styleable.precentRelativeLayout_layout_widthPrecent, widthPercent);
heightPercent = a.getFloat(R.styleable.precentRelativeLayout_layout_heightPrecent, heightPercent);
a.recycle();
}
public LayoutParams(int w, int h) {
super(w, h);
}
public LayoutParams(android.view.ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(android.widget.RelativeLayout.LayoutParams source) {
super(source);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name= "precentRelativeLayout">
<attr name="layout_widthPrecent" format="float"></attr>
<attr name="layout_heightPrecent" format="float"></attr>
</declare-styleable>
</resources>
<com.castiel.demo.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.castiel.demo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
app:layout_heightPrecent="0.2"
app:layout_widthPrecent="0.2"
android:background="#28FF28"
android:text="http://blog.csdn.net/mynameishuangshuai" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:layout_heightPrecent="0.3"
app:layout_widthPrecent="0.3"
android:background="#28FF28"
android:text="猴子搬来的救兵" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:layout_heightPrecent="0.2"
app:layout_widthPrecent="0.2"
android:background="#28FF28"
android:text="castiel" />
</com.castiel.demo.PercentRelativeLayout>