1. 在自定义View的宽和高的时候:
如何需要显示在屏幕上的 实际大小
则需要重写如下方法:
其中 mWidth, mHeight 都是全局变量 是attrs里面自己定义的
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Log.d("onMeasure",this.getWidth()+"||"+this.getHeight());
Log.d("onMeasure:onMeasure",this.getMeasuredWidth()+"||"+this.getMeasuredHeight());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 主要
setMeasuredDimension(mWidth, mHeight);
}
如果没有上面的 setMeasuredDimension(mWidth, mHeight); 即默认的情况,则自己定义的 width 和 height 不会起作用
<com.first.view.main.MyView
xmlns:myapp="http://schemas.android.com/apk/res/com.first.view.main"
android:layout_width="100dip"
android:layout_height="100dip"
myapp:height="150dip"
myapp:width="200dip" >
</com.first.view.main.MyView>