为什么我的android自定义视图不方形? [英] Why is my android custom view not square?

查看:124
本文介绍了为什么我的android自定义视图不方形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义视图,以显示正在开发的游戏的游戏板.游戏板必须始终为正方形.因此,与高度应相同.我遵循了

I created a custom view to display a gameboard for a game I'm developing. The gameboard needs to be a square at all times. So with and height should be the same. I followed this tutorial to achieve this.

我在自定义视图中添加了以下代码:

I added the following code to my custom view:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    int widthWithoutPadding = width - getPaddingLeft() - getPaddingRight();
    int heigthWithoutPadding = height - getPaddingTop() - getPaddingBottom();

    int maxWidth = (int) (heigthWithoutPadding * RATIO);
    int maxHeight = (int) (widthWithoutPadding / RATIO);

    if (widthWithoutPadding > maxWidth) {
        width = maxWidth + getPaddingLeft() + getPaddingRight();
    } else {
        height = maxHeight + getPaddingTop() + getPaddingBottom();
    }

    setMeasuredDimension(width, height);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint p = new Paint();
    p.setStyle(Paint.Style.STROKE);
    p.setStrokeWidth(3);
    canvas.drawRect(0, 0, getMeasuredWidth() - 1, getMeasuredHeight() - 1, p);
}

具有我的自定义视图的布局看起来像这样:

The layout with my custom view looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        class="com.peerkesoftware.nanograms.controls.GameBoard"
        android:id="@+id/view"
        android:background="@android:color/holo_purple"/>

</RelativeLayout>

在onDraw方法中绘制的正方形始终是正方形.很好

The square that is drawn in the onDraw method is always a square. That works fine.

在布局中,我添加了自定义视图,并为该视图提供了背景色.当我在纵向模式下将其显示在设备上时,一切正常,并且背景颜色正在填充我在onDraw方法中绘制的正方形.

In the layout I add the custom view and give the view a background color. When I display it on a device in portrait mode everything works fine and the background color is filling up the square I draw in the onDraw method.

当我将设备切换为横向模式时,正方形仍然是正方形,但是背景颜色的面积却更大.它具有相同的高度,但具有更大的宽度.但是我不知道为什么. getMeasuredWidth()和getMeasuredHeight()方法返回正确的值.这样的视角怎么会更大呢?

When I switch the device to landscape mode the square is still a square, but the area of the background color is bigger then that. It's has the same height, but has more width. But I have no idea why. The getMeasuredWidth() and getMeasuredHeight() methods return the correct values. How can it be that the view is still bigger then that?

推荐答案

由于某种原因,您的视图在RelativeLayout中不起作用.对于横向测量,最终会受到以下限制(在我的手机上):

For some reason your view doesn't work in RelativeLayout. For landscape orientation measurement ends up with following constraints (on my phone):

width = MeasureSpec:精确为404;高度= MeasureSpec:AT_MOST 388

width = MeasureSpec: EXACTLY 404; height = MeasureSpec: AT_MOST 388

由于此实际宽度最终与测量的宽度不同(在我的手机上,测量值为388,实际为404).这就是为什么您看到背景延伸到直肠之外的原因.您可以通过向onDraw和onMeasure添加一些日志记录来自己检查它.

Because of this actual width ends up different than measured width (on my phone measured=388, actual=404). That's why you see background extending beyond your rect. You can check it yourself by adding some logging to onDraw and onMeasure.

此外,尊重测量方式也无济于事,测量仍然以与上述相同的结果结束.

Also, respecting measure mode doesn't help, measurement still ends with same result as above.

解决方案是使用其他布局. LinearLayout和FrameLayout为我工作. 最好在onDraw中使用实际的视图大小以及getWidth()和getHeight().

The solution is to use a different layout. LinearLayout and FrameLayout worked for me. Also it's a good idea to use actual view size in onDraw, with getWidth() and getHeight().

如果您确实需要RelativeLayout,则可以添加一个子FrameLayout来保存视图,如下所示:

If you really need the RelativeLayout, you can add a child FrameLayout to hold your view like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <view
            android:id="@+id/view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            class="com.peerkesoftware.nanograms.controls.GameBoard"
            android:background="@android:color/holo_purple" />
    </FrameLayout>
</RelativeLayout>

这篇关于为什么我的android自定义视图不方形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆