我实现了一个可以绘画的自定义 View 。我想将它设置为 MATCH_PARENT,以便它独立于屏幕方向填充屏幕。当我将方向更改为横向时,它仅填充 50% 的宽度。
我改变了 onMeasure() 但没有效果:
public class DrawScreen extends View{
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(context.getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){
setMeasuredDimension(screenWidth, screenHeight);
}else{
setMeasuredDimension(screenHeight, screenWidth);
}
}
}
public class MyService extends Service{
...
windowManager.addView(toolbox, params);
}
最佳答案
您可以尝试以下方法:
final DrawScreen view = new DrawScreen(...);
final LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
view.setLayoutParams(lp);
关于Android:以编程方式在自定义 View 中设置 match_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905358/