一、造成原因:
由于VideoView 中的onMeasure走了自适应,导致两边无法撑满。
二、解决办法
因此,需要自定义布局,来进行重写onMeasure方法,让其两边为0即可。
public static class fullVideoView extends VideoView {
public fullVideoView(Context context) {
super(context);
}
public fullVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public fullVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public fullVideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//这里可以看到是将其设为0,不走其他的逻辑。可以去看源码原生逻辑,是还有判断的。
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
然后在布局中使用即可:
<view class="com.android.abilitytest.ActivityVideo$fullVideoView"
android:id="@+id/vv1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
三、结论
在很多不同的环境中,需要不同的自定义,因此自定义布局,很好的能解决这种问题。