【问题标题】:Android Custom View SizeAndroid 自定义视图大小
【发布时间】:2012-09-05 00:25:59
【问题描述】:

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

例如,如果在我的 XML 布局文件中,我有一个 LinearLayout,它是最高级别的视图容器,那么我希望能够添加多个我自己的椭圆形视图。 (我称之为“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 时椭圆周围的蓝色框占据了整个屏幕

标签: android xml android-layout layout


【解决方案1】:

在自定义视图的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多