Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在运行时更改自定义视图的大小_Android_Android Layout_Android Ui_Android Custom View - Fatal编程技术网

Android 在运行时更改自定义视图的大小

Android 在运行时更改自定义视图的大小,android,android-layout,android-ui,android-custom-view,Android,Android Layout,Android Ui,Android Custom View,我正在更改我的CustomView的高度和宽度,它在运行时扩展了Android视图,如下所示: @Override public void onGlobalLayout() { if(!initialized) { int containerHeight = instance.getHeight(); int containerWidth

我正在更改我的
CustomView
高度和
宽度,它在运行时扩展了Android
视图,如下所示:

        @Override
        public void onGlobalLayout() {          
            if(!initialized) {          
                int containerHeight = instance.getHeight();
                int containerWidth = instance.getWidth();
                myView.getLayoutParams().height = (int) (containerHeight * HEIGHT_RATIO);
                myView.getLayoutParams().width = (int) (containerWidth * WIDTH_RATIO);
                instance.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                initialized = true;
            }
        }
此代码位于
容器视图
构造函数中

此外,我的
CustomView
onMeasure()
如下所示:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // maximum width we should use
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }
结果是:

其中指定的
宽度
高度
I与绿色矩形的大小相同


我的问题是:为什么我的自定义视图(红色矩形)的实际大小与我在
LayoutParams
中输入的大小不一样?

不确定要为视图使用什么对象,但您可以尝试以下方法:

        @Override
        public void onGlobalLayout() {          
            if(!initialized) {          
                int containerHeight = instance.getHeight();
                int containerWidth = instance.getWidth();
                myView.getLayoutParams().height = (int) (containerHeight * HEIGHT_RATIO);
                myView.getLayoutParams().width = (int) (containerWidth * WIDTH_RATIO);
                instance.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                initialized = true;
            }
        }

CustomViewget.Window().setLayout(370480)//控制宽度和高度

我的问题出现在
自定义视图的绘图中
。在
onDraw()
中,我使用了
画布的
宽度和
高度,而不是
视图本身。

调用时可能无法正确计算大小,因此,您可能需要将代码放在处理程序中,并在post上运行它,以便在完成UI线程上的所有其他操作之后完成。请尝试以下代码

Handler handler = new Handler();
handler.post(new Runnable() {
    public void run() {
    //check sizes and update sizes here
    });

试着注释一下你的onMeasure(),我认为你不需要在它当前的实现中使用它。这并没有奏效。我使用这个实现来减少我的视图被测量的时间,因为我知道在我的情况下,大小总是精确的。我的自定义视图是一个视图,而不是一个对话框。在我的情况下…我设计和自定义视图,在X方向获得基于计时器的连续绘制。这张照片是放在HScrollView上的。你知道在这种情况下如何改变视图的大小吗!!!