Mat*_*hew 4 xml layout android android-layout
我试图在 android 中创建我自己的视图,它只是一个椭圆形,将根据典型的android:layout_width和android: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>
Run Code Online (Sandbox Code Playgroud)
名为“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();
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,上面贴出的代码的结果,第一个 PieFiller 占据了整个屏幕。(我不希望它)
在自定义视图的 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);
Run Code Online (Sandbox Code Playgroud)