将视图放在画布顶部时遇到问题

时间:2016-10-09 22:41:54

标签: android

我已经阅读/尝试了我找到的所有答案,但我没有到达任何地方。我相信我在做一些搞砸的事情,但不知道它是什么。如果我注释掉片段中的行:

group '1.0'
version '0.1-Setup'

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'com.github.bft-smart:library:master-SNAPSHOT'

}

...然后我可以看到我的双向网格视图,所以我很确定它在画布下面。  这是来自显示所有这些内容的片段:

container.addView(canvas);

和自定义画布类:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_workspace, container, false);

    bitmap = decodeImagedata(getArguments().getByteArray("image"));

    canvas = (WorkCanvas)activity.findViewById(R.id.work_canvas);
    if (canvas == null) {
        canvas = new WorkCanvas(context, null);
    }
    stickerView = new TwoWayGridView(context);
    StickerUseAdaper adapter = new StickerUseAdaper(context, R.layout.sticker_work_item, activity.getSelectedStickers());
    stickerView.setAdapter(adapter);
    container.addView(canvas);
    container.addView(stickerView);

    return view;
}

}

和布局:

class WorkCanvas extends View {


private Context context;
private Activity activity;
private Bitmap bitmap;

public WorkCanvas(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    this.activity = (Activity)context;
}

@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
            new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), null);

    //this changes nothing :-(
    View stickerView = activity.findViewById(R.id.sticker_view);
    stickerView.bringToFront();
    stickerView.invalidate();
}

public void sendBitmap(Bitmap bitmap) {
    this.bitmap= bitmap;
}

HEEEELLLLP!哦,谢谢!

1 个答案:

答案 0 :(得分:0)

好的,发现了我的愚蠢错误。尽管我对Jon Goodwin非常尊重,但我尝试的完全有可能,当我将canvas和TwoWayGridView初始化为新对象而不是抓取对xml布局文件中存在的对象的引用时,我只是犯了一个愚蠢的错误。所以:

canvas = (WorkCanvas)activity.findViewById(R.id.work_canvas);
if (canvas == null) {
    canvas = new WorkCanvas(context, null);
}

应该是:

canvas = (WorkCanvas)view.findViewById(R.id.work_canvas);

stickerView = new TwoWayGridView(context);

应该是:

stickerView = (TwoWayGridView) view.findViewById(R.id.sticker_view);

然后这很好(需要一些微调):gridView显示在画布上方。