我正在制作一个 Android 应用程序,我创建了一个带有 Canvas 的类来绘制一个矩形。我想从另一个(它的 fragment )调用那个类并显示矩形。这是我所拥有的:
这是我的 RectShape 类:
public class RectShape extends View{
public RectShape(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect ourRect = new Rect();
ourRect.set(3, 0, canvas.getWidth() - 3, 150);
Paint postColor = new Paint();
postColor.setColor(Color.WHITE);
postColor.setStyle(Paint.Style.FILL);
canvas.drawRect(ourRect, postColor);
}
}
这是我的 fragment 类:
public class FragmentFeed extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment_feed_layout, container, false);
RectShape shape = new RectShape(getActivity());
return V;
}
}
我没有收到任何错误.. 形状只是没有显示出来.. 它有什么问题吗?我不想从同一个类中调用形状。
最佳答案
您必须将 RectShape shape = new RectShape(getActivity());
添加到 fragment 中。目前你只是在初始化它。将 View 添加到 fragment 中,它将显示。
关于java - 来自外部类的 Android Canvas 不会显示在 Fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30253420/