1).我們先自定義一個類來繼承RelativeLayout,其中最主要是實現onMeasure()方法和onLayout()方法!通過generateLayoutParams()來獲取下面的attr.xml中的屬性比例值,
然后再在onMeasure()方法中 ViewGroup.LayoutParams params = child.getLayoutParams();來獲取占空比!從而達到子控件是主控件的多少比例大小!
1 package watch.devond.okhttp; 2 3 import android.content.Context; 4 import android.content.res.TypedArray; 5 import android.util.AttributeSet; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.RelativeLayout; 9 10 public class PercentRelativeLayout extends RelativeLayout { 11 12 public PercentRelativeLayout(Context context) { 13 super(context); 14 } 15 16 public PercentRelativeLayout(Context context, AttributeSet attrs) { 17 super(context, attrs); 18 } 19 20 public PercentRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { 21 super(context, attrs, defStyleAttr); 22 } 23 24 /*** 25 * 測量容器的寬高 26 * @param widthMeasureSpec 27 * @param heightMeasureSpec 28 */ 29 @Override 30 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 31 int width = View.MeasureSpec.getSize(widthMeasureSpec); 32 int height = View.MeasureSpec.getSize(heightMeasureSpec); 33 //測量子控件的寬高,然后進行改變 34 35 int childCount = getChildCount(); 36 for(int i = 0;i < childCount;i++){ 37 38 float widthPercent = 0; 39 float heightPercent = 0; 40 41 View child = getChildAt(i); 42 ViewGroup.LayoutParams params = child.getLayoutParams(); 43 if(params instanceof PercentRelativeLayout.LayoutParams){ 44 widthPercent = ((LayoutParams) params).getWidthPercent(); 45 heightPercent = ((LayoutParams) params).getHeihtPercent(); 46 } 47 if(widthPercent != 0){ 48 params.width = (int) (width * widthPercent); 49 50 params.height = (int) (height * heightPercent); 51 52 } 53 } 54 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 55 } 56 57 /*** 58 * 排版擺放控件 59 * @param changed 60 * @param l 61 * @param t 62 * @param r 63 * @param b 64 */ 65 @Override 66 protected void onLayout(boolean changed, int l, int t, int r, int b) { 67 super.onLayout(changed, l, t, r, b); 68 69 } 70 71 /** 72 * @param attrs 73 * @return 74 */ 75 @Override 76 public RelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs) { 77 78 return new LayoutParams(getContext(),attrs); 79 } 80 81 class LayoutParams extends RelativeLayout.LayoutParams { 82 83 private float widthPercent; 84 private float heihtPercent; 85 86 public float getWidthPercent() { 87 return widthPercent; 88 } 89 90 public void setWidthPercent(float widthPercent) { 91 this.widthPercent = widthPercent; 92 } 93 94 public float getHeihtPercent() { 95 return heihtPercent; 96 } 97 98 public void setHeihtPercent(float heihtPercent) { 99 this.heihtPercent = heihtPercent; 100 } 101 102 public LayoutParams(Context c, AttributeSet attrs) { 103 super(c, attrs); 104 TypedArray array = c.obtainStyledAttributes(attrs,R.styleable.PercentRelativeLayout); 105 widthPercent = array.getFloat(R.styleable.PercentRelativeLayout_layout_widthPercent,widthPercent); 106 heihtPercent = array.getFloat(R.styleable.PercentRelativeLayout_layout_heihtPercent,heihtPercent); 107 array.recycle(); 108 } 109 110 public LayoutParams(int w, int h) { 111 super(w, h); 112 } 113 114 public LayoutParams(ViewGroup.LayoutParams source) { 115 super(source); 116 } 117 118 public LayoutParams(MarginLayoutParams source) { 119 super(source); 120 } 121 122 } 123 }
2)在res文件的value文件中創建attr.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <!--百分比RelativeLayout--> <declare-styleable name="PercentRelativeLayout"> <attr name="layout_widthPercent" format="float"> </attr> <attr name="layout_heihtPercent" format="float"> </attr> </declare-styleable> </resources>
3)主界面的xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <watch.devond.okhttp.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 xmlns:app="http://schemas.android.com/apk/res-auto" 7 android:gravity="center" 8 tools:ignore="ResAuto"> 9 10 <Button 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Hello World!" 14 app:layout_widthPercent ="0.8" 15 app:layout_heihtPercent ="0.1" 16 /> 17 18 19 </watch.devond.okhttp.PercentRelativeLayout>