您的位置:首页 > 移动开发 > Android开发

Android中实现百分比布局的方式

2016-11-13 22:54 330 查看
关于android中的百分比布局,网上有各种资料,自己看完后,在这里写了个demo总结了一下.一,在布局中实现;在网上看到的最多的就是这种方式:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">

<View android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignRight="@id/strut"
android:layout_alignParentLeft="true"
android:text="Left"
android:gravity="center"
android:background="#00ffff"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignLeft="@id/strut"
android:layout_alignParentRight="true"
android:text="Right"
android:gravity="center"
android:background="#00aaff"
/>
</RelativeLayout>
不过这中方式只适用与两个控件,对于两个以上的控件,就有些力不从心了.
二. 在代码中实现:
在onCreat方法中,
ViewTreeObserver vto = rl1.getViewTreeObserver();
//可以获取到指定View的观察者,在绘制控件前的一刹那进行回调 保证能正确获取控件宽度
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (!isGetWeight){//标记  该方法会一致回调  保证这里只走一次
int w = rl1.getWidth();
Log.e("getWeight", "onCreate: ----" + w);
tv1.setWidth(w/3);
tv2.setWidth(w/3);
tv3.setWidth(w/3);
isGetWeight=true;
}
return true;
}
});
这种方式主要是监听父控件,当父控件(rl1)绘制完成后,得到父控件的宽度,然后子控件(tv1,tv2,tv3)平分父控件的宽度.
三,自定义控件:
/**
* Created by xtl1889 on 16-11-13.
*/
public class myRelativeLayout extends RelativeLayout {
public myRelativeLayout(Context context) {
super(context);
}

public myRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public myRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

/**
* 重写测量方法
*/
@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 myRelativeLayout.LayoutParams) {
widthPercent = ((LayoutParams) lp).widthPercent;
hightPercent = ((LayoutParams) lp).heightPercent;

}
Log.e("widthPercent", "onMeasure:---001- "+ widthPercent);
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);
Log.e("widthPercent", "LayoutParams: -----"+widthPercent );
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);
}

}
}
自定义属性:在attrs中定义:
<declare-styleable name= "precentRelativeLayout">
<attr name="layout_widthPrecent" format="float"></attr>
<attr name="layout_heightPrecent" format="float"></attr>
</declare-styleable>
四.百分比布局
android中已经有了百分比控件,PercentRelativeLayout(其父类是RelativeLayout)和PercentFrameLayout.
首先在build中添加:compile 'com.android.support:percent:22.2.0'
其实方法三中的代码和PercentRelativeLayout的源码很相似.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: