安卓自定义视图大小

5

我正在尝试在Android中创建自己的视图,它只是一个椭圆形,并将根据典型的 android:layout_widthandroid:layout_height 参数正确地绘制。

例如,如果在我的XML布局文件中,有一个LinearLayout是最高级别的View容器,然后我想能够添加多个我的自定义椭圆形视图(我称之为“PieFiller”)

我知道我非常接近成功,但由于某些原因,我不知道如何确定用户在XML文件中是否使用了“fill_parent”或“wrap_content”,而且我不知道是否应该询问这些信息。假设“wrap_content”将只绘制一个40x40像素的圆形。但是,如果XML布局文件指定宽度应为“fill_parent”,高度应为“wrap_content”,则希望得到一个长条形的椭圆形,其高度为40像素,宽度为屏幕上的像素数。

以下是我的布局xml文件和自定义视图的代码:

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.relativelayoutexample"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.example.relativelayoutexample.PieFiller
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        custom:labelPosition="left"
        custom:showText="true" />

    <com.example.relativelayoutexample.PieFiller
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        custom:labelPosition="left"
        custom:showText="true" />

</LinearLayout>

自定义视图类称为“PieFiller”

package com.example.relativelayoutexample;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class PieFiller extends View
{
    private boolean mShowText;
    private int mTextPos;

    private Paint paint;

    private RectF mBounds;

    public PieFiller(Context context, AttributeSet attrs)
    {
        super(context, attrs);


        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PieFiller,0,0);

        try
        {
            mShowText = a.getBoolean(R.styleable.PieFiller_showText, false);
            mTextPos = a.getInteger(R.styleable.PieFiller_labelPosition, 0);
        }
        finally
        {
            a.recycle();
        }


        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
        mBounds = new RectF();
    }

    public void onDraw(Canvas canvas)
    {
        canvas.drawCircle(40.0f, 40.0f, 40.0f, paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int minw = getSuggestedMinimumWidth();
        int w = Math.max(minw,MeasureSpec.getSize(widthMeasureSpec));

        int minh = this.getSuggestedMinimumHeight();
        int h = Math.max(minh, MeasureSpec.getSize(heightMeasureSpec));

        setMeasuredDimension(w,h);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        mBounds = new RectF(0,0,w,h);
    }

    public boolean ismShowText()
    {
        return mShowText;
    }
    public void setmShowText(boolean mShowText)
    {
        this.mShowText = mShowText;
        this.invalidate();
        this.requestLayout();
    }
    public int getmTextPos()
    {
        return mTextPos;
    }
    public void setmTextPos(int mTextPos)
    {
        this.mTextPos = mTextPos;
        this.invalidate();
        this.requestLayout();
    }



}

顺便提一下,上面发布的代码结果,第一个PieFiller占据了整个屏幕。(我不希望它这样)


对不起,我的意思不是实际的椭圆会在整个屏幕上绘制,而是另一个PieFiller甚至都没有出现,当我查看图形工具并单击第一个PieFiller时,蓝色框围绕椭圆占据了整个屏幕。 - Matthew
1个回答

6
在您的自定义视图的onMeasure()方法中添加以下逻辑:
int width = 100;
    if(getLayoutParams().width==LayoutParams.WRAP_CONTENT)
    {
        width = 40;
    }
    else if((getLayoutParams().width==LayoutParams.MATCH_PARENT)||(getLayoutParams().width==LayoutParams.FILL_PARENT))
    {
        width = MeasureSpec.getSize(widthMeasureSpec);
    }
    else
        width = getLayoutParams().width;


    int height = 100;
    if(getLayoutParams().height==LayoutParams.WRAP_CONTENT)
    {

    }
    else if((getLayoutParams().height==LayoutParams.MATCH_PARENT)||(getLayoutParams().height==LayoutParams.FILL_PARENT))
    {
        height = MeasureSpec.getSize(heightMeasureSpec);
    }
    else
        height = getLayoutParams().height;


    setMeasuredDimension(width|MeasureSpec.EXACTLY, height|MeasureSpec.EXACTLY);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接