我使用以下代码将agraph类生成的图形加载到R.id.TranFrag容器中。但它在执行时没有显示任何内容。请帮忙
Agraph agraph = new Agraph(getActivity(),30);
LinearLayout mainContainer = (LinearLayout) getActivity().findViewById(R.id.TranFrag);
mainContainer.addView(agraph);
其中Agraph.java如下
package com.salgaonkar.roomies_v3;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class Agraph extends View {
private Paint paint;
private float MaxY = 20;
float horstart = 0;
public Agraph(Context context , float MaxY) {
super(context);
paint = new Paint();
this.MaxY = MaxY;
setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas) {
float Height = getHeight()-20;
float Width = getWidth();
canvas.drawARGB(255, 255, 255, 255);
for (int i=0; i<=10; i++) {
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(20.0f);
float y = ((Height / 10) * i)+20;
Log.d("y: ",""+y);
Log.d("lable", ""+(MaxY)*(10-i)/10);
canvas.drawText(String.valueOf((MaxY)*(10-i)/10),0,y,paint);
}
}
}
答案 0 :(得分:0)
似乎视图Agraph没有实现onMeasure(int,int)方法。 尝试添加类似的内容:
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(100, 100);
}
答案 1 :(得分:0)
好的我创建了一个示例项目,现在我调用片段活动并将主活动上下文作为参数传递,在片段中我创建一个构造函数调用Agraph并添加视图。 在活动中
Fragment f = new TestFragment(this);
和片段
public TestFragment(FExampleActivity fExampleActivity) {
Agraph agraph = new Agraph(fExampleActivity,30);
LinearLayout mainContainer = (LinearLayout)fExampleActivity.findViewById(R.id.frags);
mainContainer.addView(agraph);
}
这为我工作..,。
答案 2 :(得分:0)
Agraph agraph = new Agraph(getActivity(),30);
LinearLayout mainContainer = (LinearLayout) getActivity().findViewById(R.id.TranFrag);
mainContainer.addView(agraph);
好吧现在我发现上面代码中的错误实际上它在容器中添加了视图但由于容器已经
而无法看到填充了ListView,所以我改变了这样的代码
Agraph agraph = new Agraph(getActivity(),30);
LinearLayout mainContainer = (LinearLayout) getActivity().findViewById(R.id.TranFrag);
mainContainer.removeAllViews(); //added
mainContainer.addView(agraph);
现在它运作正常。