问题描述
我试图创造的android我自己的看法这简直是一个椭圆形的,将根据典型的机器人绘制正确:layout_width 和的android:layout_height 参数。
I am attempting to create my own view in android which is simply an oval and will be drawn correctly based on the typical android:layout_width and android:layout_height parameters.
因此,举例来说,如果在我的XML布局文件,我有一个的LinearLayout这是最高级别的View容器,然后,我希望能够加入我自己的意见椭圆形多。 (我称之为PieFiller)
So for example, if in my XML layout file, I have a LinearLayout which is the highest level View Container, I then want to be able to add multiple of my own oval Views. (which I called "PieFiller")
我知道我非常接近,但由于某些原因,我不知道如何确定如果XML文件中的用户说,FILL_PARENT或WRAP_CONTENT我不知道我是否应该甚至可以要求该信息。让我们假设WRAP_CONTENT只会画一个圆是40×40像素。但是,如果XML布局文件指定宽度应FILL_PARENT,高度WRAP_CONTENT我要很长的横椭圆即40像素高,在屏幕的像素数。
I know I am very close, but for some reason, I do not know how to determine if the user in the XML file said "fill_parent" or "wrap_content" and I don't know if I should even be asking for that information. Lets assume that wrap_content would simply draw a circle that is 40x40 pixels. But, if the XML layout file specifies that the width should "fill_parent" and the height "wrap_content" I want a long horizontal oval that is 40 pixels high and the number of pixels of the screen across.
下面是我的布局XML文件中的code和我的自定义视图:
Below is the code from my layout xml file and my custom view:
布局文件
<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();
}
}
顺便说一下,在code的结果上面贴,第一PieFiller占据整个屏幕。 (我不希望它)
By the way, the result of the code posted above, the first PieFiller takes up the entire screen. (which I don't want it to)
推荐答案
在onMeasure()自定义视图中添加以下的逻辑:
Inside onMeasure() of your custom view add below logic:
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);
这篇关于Android的自定义视图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!