我试图在android中创建我自己的视图,它只是一个椭圆形,将根据典型的 android:layout_width 和 android: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占据了整个屏幕。 (我不想要它)
答案 0 :(得分:4)
在自定义视图的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);