1.getLayoutParams().width
getLayoutParams().width可以在onMesure()方法中获取 。
若在xml文件中定义了该view的具体宽度,如android:layout_width="500dp",view.getLayoutParams().width就为500;
若定义为android:layout_width="match_parent",则view.getLayoutParams().width就为-1;
若定义为android:layout_width="wrap_content",则view.getLayoutParams().width就为-2;
public static final int FILL_PARENT = -1;
public static final int MATCH_PARENT = -1;
public static final int WRAP_CONTENT = -2;
getLayoutParams().width返回的是该view向父view请求的最大宽度,应该是接近实际宽度,可能不是view实际绘画的宽度。
ps:
1). getLayoutParams()方法的调用必须要有父控件,否则返回的就是null对象。该方法返回的是该控件的LayoutParams对象。
/**
* Get the LayoutParams associated with this view. All views should have
* layout parameters. These supply parameters to the <i>parent</i> of this
* view specifying how it should be arranged. There are many subclasses of
* ViewGroup.LayoutParams, and these correspond to the different subclasses
* of ViewGroup that are responsible for arranging their children.
*
* This method may return null if this View is not attached to a parent
* ViewGroup or {@link #setLayoutParams(android.view.Vie